Node JS Path Module
Written By: Avinash Malhotra
Updated on
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.
const path=require('path');
console.log(path.normalize('./src'));
const path=require('path');
console.log(path.normalize('src//img'));
const path=require('path');
console.log(path.normalize('./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.
const path=require('path');
console.log(path.basename('./views/'));
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.
const path=require('path');
console.log(path.basename('./views/index.html','.html'));
Win32
const path=require('path');
console.log(path.basename('C:\\temp\\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/'));
const path=require('path');
console.log(path.dirname('./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.
const path=require('path');
console.log(path.extname('./views/index.html'));
const path=require('path');
console.log(path.extname('./assets/css/style.css'));
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,
const path=require('path');
console.log(path.resolve(__filename));
const path=require('path');
console.log(path.resolve('src'));
const path=require('path');
console.log(path.resolve('views'));
path.resolve with two argumets
const path=require('path');
console.log(path.resolve('src','views'));
const path=require('path');
console.log(path.resolve('src/assets','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','..'));