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');
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.
import path from 'node:path';
console.log(path.normalize('./src'));
import path from 'node:path';
console.log(path.normalize('src//img'));
import path from 'node:path';
console.log(path.normalize('./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.
import path from 'node:path';
console.log(path.basename('./views/'));
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.
import path from 'node:path';
console.log(path.basename('./views/index.html','.html'));
Win32
import path from 'node:path';
console.log(path.basename('C:\\temp\\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/'));
import path from 'node:path';
console.log(path.dirname('./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.
import path from 'node:path';
console.log(path.extname('./views/index.html'));
import path from 'node:path';
console.log(path.extname('./assets/css/style.css'));
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,
import path from 'node:path';
console.log(path.resolve(__filename));
import path from 'node:path';
console.log(path.resolve('src'));
import path from 'node:path';
console.log(path.resolve('views'));
path.resolve with two argumets
import path from 'node:path';
console.log(path.resolve('src','views'));
import path from 'node:path';
console.log(path.resolve('src/assets','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','..'));