Path Module

Path module is a build in module in node js to work with files and directories. The default path operation of Windows and UNIX based OS like MacOS can very. For exp, Windows OS use backward slash '\' when using path.join() method, and Linux / Mac OS use forward slash '/' when using path.join(). To resolve this type of path inconsistency, path module is required.

Include Path Module

         
    const path=require('path');
         
    import path from 'node:path';

path.normalize

path.normalize method with string arguments, fixed up slash, resolve . and .. in path and also remove duplicate slashes.

src
              
    import path from 'node:path';
    console.log(path.normalize('./src'));
/src/img
              
    import path from 'node:path';
    console.log(path.normalize('src//img'));
src/
              
    import path from 'node:path';
    console.log(path.normalize('./src/'));
/src
              
    import path from 'node:path';
    console.log(path.normalize('src/img/..'));

path.basename

path.basename methods returns the name of last file or directory. For folder, it will return name of last folder and for file, it will return name and extension of file.

views
              
    import path from 'node:path';
    console.log(path.basename('./views/'));
index.html
              
    import path from 'node:path';
    console.log(path.basename('./views/index.html'));

Get filename without extension

To get last filename without extension, use .extension as second argument in basename method.

index
              
    import path from 'node:path';
    console.log(path.basename('./views/index.html','.html'));

Win32

C:\\temp\\myfile.html
              
    import path from 'node:path';
    console.log(path.basename('C:\\temp\\myfile.html'));
myfile.html
              
    import path from 'node:path';
    console.log(path.win32.basename('C:\\temp\\myfile.html'));

path.dirname

path.dirname method returns directory name of path.

.
       
    import path from 'node:path';
    console.log(path.dirname('./src/'));
.src
       
    import path from 'node:path';
    console.log(path.dirname('./src/views'));
./src/views
       
    import path from 'node:path';
    console.log(path.dirname('./src/views/index.html'));

path.extname

path.extname method returns extension of file in path.

.html
       
    import path from 'node:path';
    console.log(path.extname('./views/index.html'));
.css
       
    import path from 'node:path';
    console.log(path.extname('./assets/css/style.css'));
.js
       
    import path from 'node:path';
    console.log(path.extname('./src/server.js'));

path.resolve

path.resolve return complete absolute path of file if single argument is passed. For more than one arguments,

/Users/avinashmalhotra/Desktop/node/src/test.js
       
    import path from 'node:path';
    console.log(path.resolve(__filename));
/Users/avinashmalhotra/Desktop/node/src
       
    import path from 'node:path';
    console.log(path.resolve('src'));
/Users/avinashmalhotra/Desktop/node/src/views
       
    import path from 'node:path';
    console.log(path.resolve('views'));

path.resolve with two argumets

/Users/avinashmalhotra/Desktop/node/src/views
       
    import path from 'node:path';
    console.log(path.resolve('src','views'));
/Users/avinashmalhotra/Desktop/node/src/assets/css/scss
       
    import path from 'node:path';
    console.log(path.resolve('src/assets','css/scss'));
/css/scss
       
    import path from 'node:path';
    console.log(path.resolve('/src','/css','scss'));

path.join

path.join joins all arguments and then normalize the resulting path. The separator used to join is based on OS Platform.

/foo/bar

       
    import path from 'node:path';
    console.log(path.join('/foo','bar'));

/foo/bar

       
    import path from 'node:path';
    console.log(path.join('foo','bar','.'));

/foo

       
    import path from 'node:path';
    console.log(path.join('foo','bar','..'));