Node.js Tutorial – Path

The tutorial provides the details on the node.js path module. The node.js path module provides the utilizes for working with the file path and directory path. The node.js path module can be accessed using the command

const path = require('path');

There will be path variation when node.js is running on different operating system. To achieve same path execution output path.win32 should be used.

Node.js Path methods

Path MethodPath Method Description
path.basename(path[, ext])execution behaviour is similar to unix base command where it returns the last portion of the path
path – <string>
ext – <string> file extension (optional)
returns – <string>
path.basename(‘/tmp/nodejs/path/pathbasename.html’);
// Returns: ‘pathbasename.html’
path.delimiteruses the type as <string> and provides different path specific delimiter
; for windows
: for POSIX
console.log(process.env.PATH);
// Returns: ‘/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin’
console.log(process.env.PATH);
// Returns: ‘C:\Windows\system32;C:\Windows;C:\Program Files\node\’
path.dirname(path) execution behaviour is similar to unix base command where it returns the directory name of the path
path – <string>
returns – <string>
path.dirname(‘/tmp/nodejs/path/pathdir.html’);
// Returns: ‘/tmp/nodejs/path’
path.extname(path) execution behaviour is similar to unix base command where it returns the extension of the path
path – <string>
returns – <string>
path.dirname(‘/tmp/nodejs/path/pathdir.html’);
// Returns: ‘.html’
path.isabsolute(path) indicates if path is an absolute path.
path – <string>
return – boolean
path.isAbsolute(‘//nodeServer’); // true
path.isAbsolute(‘node/path’); // false
path.isAbsolute(‘.’); // false
path.relative(from, to) solves the relative path from “from” to “to”
from <string>
to <string>
return <string>
path.relative(‘/data/node/path/relative’, ‘/data/node/tmp/absolute’);
// Returns: ‘../../tmp/absolute’
path.join([path1][, path2][, …]) join all path segments using delimiter and returns the normalized resulting path
path – <string> –sequence of path segments
returns – <string> — normalized resulting path
path.join(‘/tmp’, ‘node’, ‘path/sample’, ‘..’);
// Returns: ‘/tmp/node/path/sample’

path.normalize(p)
normalize a string path, taking care of ‘..’ and ‘.’ parts
path – <string>
returns – <string>
path.normalize(‘C:\tmp\\node\path\..\’);
// Returns: ‘C:\tmp\node\’
path.parse(pathstring) returns the object from the path string
path – <string>
returns – <string> where return object could have
dir <string>
root <string>
base <string>
name <string>
ext <string>
On Windows:
path.parse(‘C:\tmp\node\pathParsing.txt’);
// Returns:
// { root: ‘C:\’,
// dir: ‘C:\temp\node’,
// base: ‘pathParsing.txt’,
// ext: ‘.txt’,
// name: ‘file’ }
path.format(pathObject)returns the path string from the object where pathObject could have
dir <string>
root <string>
base <string>
name <string>
ext <string>
pathObject.root is ignored if pathObject.dir is provided
pathObject.ext and pathObject.name are ignored if pathObject.base exists
On Windows:
path.format({
dir: ‘C:\node\pathdir’,
base: ‘pathexample.txt’
});
// Returns: ‘C:\node\pathdir\pathexample.txt’