Path - Node - Js v12.10.0 Documentation
Path - Node - Js v12.10.0 Documentation
0 Documentation
Path
Stability: 2 - Stable
The path module provides utilities for working with file and directory paths.
It can be accessed using:
On POSIX:
path.basename('C:\\temp\\myfile.html');
// Returns: 'C:\\temp\\myfile.html'
On Windows:
path.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
To achieve consistent results when working with Windows file paths on any
operating system, use path.win32 :
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 1/11
03/08/2021 Path | Node.js v12.10.0 Documentation
path.win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
To achieve consistent results when working with POSIX file paths on any
operating system, use path.posix :
path.posix.basename('/tmp/myfile.html');
// Returns: 'myfile.html'
path.basename(path[, ext])
path <string>
Returns: <string>
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
path.delimiter
<string>
; for Windows
: for POSIX
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 2/11
03/08/2021 Path | Node.js v12.10.0 Documentation
console.log(process.env.PATH);
// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter);
On Windows:
console.log(process.env.PATH);
process.env.PATH.split(path.delimiter);
path.dirname(path)
path <string>
Returns: <string>
path.dirname('/foo/bar/baz/asdf/quux');
// Returns: '/foo/bar/baz/asdf'
path.extname(path)
path <string>
Returns: <string>
The path.extname() method returns the extension of the path , from the last
occurrence of the . (period) character to end of string in the last portion of
the path . If there is no . in the last portion of
the path , or if
there are no . characters other than the first character of
the basename of path (see path.basename() ) , an empty string is returned.
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 3/11
03/08/2021 Path | Node.js v12.10.0 Documentation
path.extname('index.html');
// Returns: '.html'
path.extname('index.coffee.md');
// Returns: '.md'
path.extname('index.');
// Returns: '.'
path.extname('index');
// Returns: ''
path.extname('.index');
// Returns: ''
path.extname('.index.md');
// Returns: '.md'
path.format(pathObject)
pathObject <Object>
dir <string>
root <string>
base <string>
name <string>
ext <string>
Returns: <string>
The path.format() method returns a path string from an object. This is the
opposite of path.parse() .
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 4/11
03/08/2021 Path | Node.js v12.10.0 Documentation
// `${dir}${path.sep}${base}`
path.format({
root: '/ignored',
dir: '/home/user/dir',
base: 'file.txt'
});
// Returns: '/home/user/dir/file.txt'
path.format({
root: '/',
base: 'file.txt',
ext: 'ignored'
});
// Returns: '/file.txt'
path.format({
root: '/',
name: 'file',
ext: '.txt'
});
// Returns: '/file.txt'
On Windows:
path.format({
dir: 'C:\\path\\dir',
base: 'file.txt'
});
// Returns: 'C:\\path\\dir\\file.txt'
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 5/11
03/08/2021 Path | Node.js v12.10.0 Documentation
path.isAbsolute(path)
path <string>
Returns: <boolean>
path.isAbsolute('/foo/bar'); // true
path.isAbsolute('/baz/..'); // true
path.isAbsolute('qux/'); // false
path.isAbsolute('.'); // false
On Windows:
path.isAbsolute('//server'); // true
path.isAbsolute('\\\\server'); // true
path.isAbsolute('C:/foo/..'); // true
path.isAbsolute('C:\\foo\\..'); // true
path.isAbsolute('bar\\baz'); // false
path.isAbsolute('bar/baz'); // false
path.isAbsolute('.'); // false
path.join([...paths])
...paths <string> A sequence of path segments
Returns: <string>
The path.join() method joins all given path segments together using the
platform-specific separator as a delimiter, then normalizes the resulting path.
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 6/11
03/08/2021 Path | Node.js v12.10.0 Documentation
// Returns: '/foo/bar/baz/asdf'
path.normalize(path)
path <string>
Returns: <string>
The path.normalize() method normalizes the given path , resolving '..' and
'.' segments.
When multiple, sequential path segment separation characters are found (e.g.
/ on POSIX and either \ or / on Windows), they are replaced by a single
instance of the platform-specific path segment
separator ( / on POSIX and
\ on Windows). Trailing separators are preserved.
path.normalize('/foo/bar//baz/asdf/quux/..');
// Returns: '/foo/bar/baz/asdf'
On Windows:
path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// Returns: 'C:\\temp\\foo\\'
path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
// Returns: 'C:\\temp\\foo\\bar'
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 7/11
03/08/2021 Path | Node.js v12.10.0 Documentation
path.parse(path)
path <string>
Returns: <Object>
dir <string>
root <string>
base <string>
name <string>
ext <string>
path.parse('/home/user/dir/file.txt');
// Returns:
// { root: '/',
// dir: '/home/user/dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file' }
┌─────────────────────┬────────────┐
│ dir │ base │
├──────┬ ├──────┬─────┤
└──────┴──────────────┴──────┴─────┘
(all spaces in the "" line should be ignored — they are purely for formatting)
On Windows:
path.parse('C:\\path\\dir\\file.txt');
// Returns:
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 8/11
03/08/2021 Path | Node.js v12.10.0 Documentation
// { root: 'C:\\',
// dir: 'C:\\path\\dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file' }
┌─────────────────────┬────────────┐
│ dir │ base │
├──────┬ ├──────┬─────┤
└──────┴──────────────┴──────┴─────┘
(all spaces in the "" line should be ignored — they are purely for formatting)
path.posix
<Object>
path.relative(from, to)
from <string>
to <string>
Returns: <string>
The path.relative() method returns the relative path from from to to based
on the current working directory. If from and to each resolve to the same
path (after calling path.resolve() on each),
a zero-length string is returned.
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// Returns: '../../impl/bbb'
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 9/11
03/08/2021 Path | Node.js v12.10.0 Documentation
On Windows:
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// Returns: '..\\..\\impl\\bbb'
path.resolve([...paths])
...paths <string> A sequence of paths or path segments
Returns: <string>
The given sequence of paths is processed from right to left, with each
subsequent path prepended until an absolute path is constructed.
For instance, given the sequence of path segments: /foo , /bar ,
baz ,
calling path.resolve('/foo', '/bar', 'baz') would return /bar/baz .
If after processing all given path segments an absolute path has not yet
been generated, the current working directory is used.
The resulting path is normalized and trailing slashes are removed unless the
path is resolved to the root directory.
If no path segments are passed, path.resolve() will return the absolute path
of the current working directory.
path.resolve('/foo/bar', './baz');
// Returns: '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/');
// Returns: '/tmp/file'
path.sep
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 10/11
03/08/2021 Path | Node.js v12.10.0 Documentation
<string>
\ on Windows
/ on POSIX
'foo/bar/baz'.split(path.sep);
On Windows:
'foo\\bar\\baz'.split(path.sep);
// Returns: ['foo', 'bar', 'baz']
On Windows, both the forward slash ( / ) and backward slash ( \ ) are accepted
as path segment separators; however, the path methods only add backward
slashes ( \ ).
path.toNamespacedPath(path)
path <string>
Returns: <string>
path.win32
<Object>
file:///C:/Users/Bahiense/AppData/Roaming/npm/node_modules/learnyounode/docs-nodejs/path.html 11/11