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');

path.normalize

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

/src
              
    const path=require('path');
    console.log(path.normalize('./src'));
/src/img
              
    const path=require('path');
    console.log(path.normalize('src//img'));
src/
              
    const path=require('path');
    console.log(path.normalize('./src/'));
/src
              
    const path=require('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
              
    const path=require('path');
    console.log(path.basename('./views/'));
index.html
              
    const path=require('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
              
    const path=require('path');
    console.log(path.basename('./views/index.html','.html'));

Win32

C:\\temp\\myfile.html
              
    const path=require('path');
    console.log(path.basename('C:\\temp\\myfile.html'));
myfile.html
              
    const path=require('path');
    console.log(path.win32.basename('C:\\temp\\myfile.html'));

path.dirname

path.dirname method returns directory name of path.

.
       
    const path=require('path');
    console.log(path.dirname('./src/'));
.src
       
    const path=require('path');
    console.log(path.dirname('./src/views'));
./src/views
       
    const path=require('path');
    console.log(path.dirname('./src/views/index.html'));

path.extname

path.extname method returns extension of file in path.

.html
       
    const path=require('path');
    console.log(path.extname('./views/index.html'));
.css
       
    const path=require('path');
    console.log(path.extname('./assets/css/style.css'));
.js
       
    const path=require('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
       
    const path=require('path');
    console.log(path.resolve(__filename));
/Users/avinashmalhotra/Desktop/node/src
       
    const path=require('path');
    console.log(path.resolve('src'));
/Users/avinashmalhotra/Desktop/node/src/views
       
    const path=require('path');
    console.log(path.resolve('views'));

path.resolve with two argumets

/Users/avinashmalhotra/Desktop/node/src/views
       
    const path=require('path');
    console.log(path.resolve('src','views'));
/Users/avinashmalhotra/Desktop/node/src/assets/css/scss
       
    const path=require('path');
    console.log(path.resolve('src/assets','css/scss'));
/css/scss
       
    const path=require('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

       
    const path=require('path');
    console.log(path.join('/foo','bar'));

/foo/bar

       
    const path=require('path');
    console.log(path.join('foo','bar','.'));

/foo

       
    const path=require('path');
    console.log(path.join('foo','bar','..'));