The Node.js path module
TABLE OF CONTENTS
The path
module provides a lot of very useful functionality to access and interact with the file system.
There is no need to install it. Being part of the Node.js core, it can be used by simply requiring it:
JSconst path = require('path');
This module provides path.sep
which provides the path segment separator (\
on Windows, and /
on Linux / macOS), and path.delimiter
which provides the path delimiter (;
on Windows, and :
on Linux / macOS).
These are the path
methods:
path.basename()
Return the last portion of a path. A second parameter can filter out the file extension:
JSrequire('path').basename('/test/something'); // somethingrequire('path').basename('/test/something.txt'); // something.txtrequire('path').basename('/test/something.txt', '.txt'); // something
path.dirname()
Return the directory part of a path:
JSrequire('path').dirname('/test/something'); // /testrequire('path').dirname('/test/something/file.txt'); // /test/something
path.extname()
Return the extension part of a path
JSrequire('path').extname('/test/something'); // ''require('path').extname('/test/something/file.txt'); // '.txt'
path.format()
Returns a path string from an object, This is the opposite of path.parse
path.format
accepts an object as argument with the following keys:
root
: the rootdir
: the folder path starting from the rootbase
: the file name + extensionname
: the file nameext
: the file extension
root
is ignored if dir
is provided
ext
and name
are ignored if base
exists
JS// POSIXrequire('path').format({ dir: '/Users/joe', base: 'test.txt' }); // '/Users/joe/test.txt'require('path').format({ root: '/Users/joe', name: 'test', ext: '.txt' }); // '/Users/joe/test.txt'// WINDOWSrequire('path').format({ dir: 'C:\\Users\\joe', base: 'test.txt' }); // 'C:\\Users\\joe\\test.txt'
path.isAbsolute()
Returns true if it's an absolute path
JSrequire('path').isAbsolute('/test/something'); // truerequire('path').isAbsolute('./test/something'); // false
path.join()
Joins two or more parts of a path:
JSconst name = 'joe';require('path').join('/', 'users', name, 'notes.txt'); // '/users/joe/notes.txt'
path.normalize()
Tries to calculate the actual path when it contains relative specifiers like .
or ..
, or double slashes:
JSrequire('path').normalize('/users/joe/..//test.txt'); // '/users/test.txt'
path.parse()
Parses a path to an object with the segments that compose it:
root
: the rootdir
: the folder path starting from the rootbase
: the file name + extensionname
: the file nameext
: the file extension
Example:
JSrequire('path').parse('/users/test.txt');
results in
CONSOLE{root: '/',dir: '/users',base: 'test.txt',ext: '.txt',name: 'test'}
path.relative()
Accepts 2 paths as arguments. Returns the relative path from the first path to the second, based on the current working directory.
Example:
JSrequire('path').relative('/Users/joe', '/Users/joe/test.txt'); // 'test.txt'require('path').relative('/Users/joe', '/Users/joe/something/test.txt'); // 'something/test.txt'
path.resolve()
You can get the absolute path calculation of a relative path using path.resolve()
:
JSrequire('path').resolve('joe.txt'); // '/Users/joe/joe.txt' if run from my home folder
By specifying a second parameter, resolve
will use the first as a base for the second:
JSrequire('path').resolve('tmp', 'joe.txt'); // '/Users/joe/tmp/joe.txt' if run from my home folder
If the first parameter starts with a slash, that means it's an absolute path:
JSrequire('path').resolve('/etc', 'joe.txt'); // '/etc/joe.txt'