Source Code: lib/path.js
The node:path module provides utilities for working with file and directory\npaths. It can be accessed using:
node:path
const path = require('node:path');\n
The default operation of the node:path module varies based on the operating\nsystem on which a Node.js application is running. Specifically, when running on\na Windows operating system, the node:path module will assume that\nWindows-style paths are being used.
So using path.basename() might yield different results on POSIX and Windows:
path.basename()
On POSIX:
path.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'C:\\\\temp\\\\myfile.html'\n
On Windows:
path.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'myfile.html'\n
To achieve consistent results when working with Windows file paths on any\noperating system, use path.win32:
path.win32
On POSIX and Windows:
path.win32.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'myfile.html'\n
To achieve consistent results when working with POSIX file paths on any\noperating system, use path.posix:
path.posix
path.posix.basename('/tmp/myfile.html');\n// Returns: 'myfile.html'\n
On Windows Node.js follows the concept of per-drive working directory.\nThis behavior can be observed when using a drive path without a backslash. For\nexample, path.resolve('C:\\\\') can potentially return a different result than\npath.resolve('C:'). For more information, see\nthis MSDN page.
path.resolve('C:\\\\')
path.resolve('C:')
The path.basename() method returns the last portion of a path, similar to\nthe Unix basename command. Trailing directory separators are\nignored.
path
basename
path.basename('/foo/bar/baz/asdf/quux.html');\n// Returns: 'quux.html'\n\npath.basename('/foo/bar/baz/asdf/quux.html', '.html');\n// Returns: 'quux'\n
Although Windows usually treats file names, including file extensions, in a\ncase-insensitive manner, this function does not. For example, C:\\\\foo.html and\nC:\\\\foo.HTML refer to the same file, but basename treats the extension as a\ncase-sensitive string:
C:\\\\foo.html
C:\\\\foo.HTML
path.win32.basename('C:\\\\foo.html', '.html');\n// Returns: 'foo'\n\npath.win32.basename('C:\\\\foo.HTML', '.html');\n// Returns: 'foo.HTML'\n
A TypeError is thrown if path is not a string or if suffix is given\nand is not a string.
TypeError
suffix
The path.dirname() method returns the directory name of a path, similar to\nthe Unix dirname command. Trailing directory separators are ignored, see\npath.sep.
path.dirname()
dirname
path.sep
path.dirname('/foo/bar/baz/asdf/quux');\n// Returns: '/foo/bar/baz/asdf'\n
A TypeError is thrown if path is not a string.
The path.extname() method returns the extension of the path, from the last\noccurrence of the . (period) character to end of string in the last portion of\nthe path. If there is no . in the last portion of the path, or if\nthere are no . characters other than the first character of\nthe basename of path (see path.basename()) , an empty string is returned.
path.extname()
.
path.extname('index.html');\n// Returns: '.html'\n\npath.extname('index.coffee.md');\n// Returns: '.md'\n\npath.extname('index.');\n// Returns: '.'\n\npath.extname('index');\n// Returns: ''\n\npath.extname('.index');\n// Returns: ''\n\npath.extname('.index.md');\n// Returns: '.md'\n
The path.format() method returns a path string from an object. This is the\nopposite of path.parse().
path.format()
path.parse()
When providing properties to the pathObject remember that there are\ncombinations where one property has priority over another:
pathObject
pathObject.root
pathObject.dir
pathObject.ext
pathObject.name
pathObject.base
For example, on POSIX:
// If `dir`, `root` and `base` are provided,\n// `${dir}${path.sep}${base}`\n// will be returned. `root` is ignored.\npath.format({\n root: '/ignored',\n dir: '/home/user/dir',\n base: 'file.txt'\n});\n// Returns: '/home/user/dir/file.txt'\n\n// `root` will be used if `dir` is not specified.\n// If only `root` is provided or `dir` is equal to `root` then the\n// platform separator will not be included. `ext` will be ignored.\npath.format({\n root: '/',\n base: 'file.txt',\n ext: 'ignored'\n});\n// Returns: '/file.txt'\n\n// `name` + `ext` will be used if `base` is not specified.\npath.format({\n root: '/',\n name: 'file',\n ext: '.txt'\n});\n// Returns: '/file.txt'\n
path.format({\n dir: 'C:\\\\path\\\\dir',\n base: 'file.txt'\n});\n// Returns: 'C:\\\\path\\\\dir\\\\file.txt'\n
The path.isAbsolute() method determines if path is an absolute path.
path.isAbsolute()
If the given path is a zero-length string, false will be returned.
false
path.isAbsolute('/foo/bar'); // true\npath.isAbsolute('/baz/..'); // true\npath.isAbsolute('qux/'); // false\npath.isAbsolute('.'); // false\n
path.isAbsolute('//server'); // true\npath.isAbsolute('\\\\\\\\server'); // true\npath.isAbsolute('C:/foo/..'); // true\npath.isAbsolute('C:\\\\foo\\\\..'); // true\npath.isAbsolute('bar\\\\baz'); // false\npath.isAbsolute('bar/baz'); // false\npath.isAbsolute('.'); // false\n
The path.join() method joins all given path segments together using the\nplatform-specific separator as a delimiter, then normalizes the resulting path.
path.join()
Zero-length path segments are ignored. If the joined path string is a\nzero-length string then '.' will be returned, representing the current\nworking directory.
'.'
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');\n// Returns: '/foo/bar/baz/asdf'\n\npath.join('foo', {}, 'bar');\n// Throws 'TypeError: Path must be a string. Received {}'\n
A TypeError is thrown if any of the path segments is not a string.
The path.normalize() method normalizes the given path, resolving '..' and\n'.' segments.
path.normalize()
'..'
When multiple, sequential path segment separation characters are found (e.g.\n/ on POSIX and either \\ or / on Windows), they are replaced by a single\ninstance of the platform-specific path segment separator (/ on POSIX and\n\\ on Windows). Trailing separators are preserved.
/
\\
If the path is a zero-length string, '.' is returned, representing the\ncurrent working directory.
path.normalize('/foo/bar//baz/asdf/quux/..');\n// Returns: '/foo/bar/baz/asdf'\n
path.normalize('C:\\\\temp\\\\\\\\foo\\\\bar\\\\..\\\\');\n// Returns: 'C:\\\\temp\\\\foo\\\\'\n
Since Windows recognizes multiple path separators, both separators will be\nreplaced by instances of the Windows preferred separator (\\):
path.win32.normalize('C:////temp\\\\\\\\/\\\\/\\\\/foo/bar');\n// Returns: 'C:\\\\temp\\\\foo\\\\bar'\n
The path.parse() method returns an object whose properties represent\nsignificant elements of the path. Trailing directory separators are ignored,\nsee path.sep.
The returned object will have the following properties:
dir
root
base
name
ext
path.parse('/home/user/dir/file.txt');\n// Returns:\n// { root: '/',\n// dir: '/home/user/dir',\n// base: 'file.txt',\n// ext: '.txt',\n// name: 'file' }\n
┌─────────────────────┬────────────┐\n│ dir │ base │\n├──────┬ ├──────┬─────┤\n│ root │ │ name │ ext │\n\" / home/user/dir / file .txt \"\n└──────┴──────────────┴──────┴─────┘\n(All spaces in the \"\" line should be ignored. They are purely for formatting.)\n
path.parse('C:\\\\path\\\\dir\\\\file.txt');\n// Returns:\n// { root: 'C:\\\\',\n// dir: 'C:\\\\path\\\\dir',\n// base: 'file.txt',\n// ext: '.txt',\n// name: 'file' }\n
┌─────────────────────┬────────────┐\n│ dir │ base │\n├──────┬ ├──────┬─────┤\n│ root │ │ name │ ext │\n\" C:\\ path\\dir \\ file .txt \"\n└──────┴──────────────┴──────┴─────┘\n(All spaces in the \"\" line should be ignored. They are purely for formatting.)\n
The path.relative() method returns the relative path from from to to based\non the current working directory. If from and to each resolve to the same\npath (after calling path.resolve() on each), a zero-length string is returned.
path.relative()
from
to
path.resolve()
If a zero-length string is passed as from or to, the current working\ndirectory will be used instead of the zero-length strings.
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');\n// Returns: '../../impl/bbb'\n
path.relative('C:\\\\orandea\\\\test\\\\aaa', 'C:\\\\orandea\\\\impl\\\\bbb');\n// Returns: '..\\\\..\\\\impl\\\\bbb'\n
A TypeError is thrown if either from or to is not a string.
The path.resolve() method resolves a sequence of paths or path segments into\nan absolute path.
The given sequence of paths is processed from right to left, with each\nsubsequent path prepended until an absolute path is constructed.\nFor instance, given the sequence of path segments: /foo, /bar, baz,\ncalling path.resolve('/foo', '/bar', 'baz') would return /bar/baz\nbecause 'baz' is not an absolute path but '/bar' + '/' + 'baz' is.
/foo
/bar
baz
path.resolve('/foo', '/bar', 'baz')
/bar/baz
'baz'
'/bar' + '/' + 'baz'
If, after processing all given path segments, an absolute path has not yet\nbeen generated, the current working directory is used.
The resulting path is normalized and trailing slashes are removed unless the\npath is resolved to the root directory.
Zero-length path segments are ignored.
If no path segments are passed, path.resolve() will return the absolute path\nof the current working directory.
path.resolve('/foo/bar', './baz');\n// Returns: '/foo/bar/baz'\n\npath.resolve('/foo/bar', '/tmp/file/');\n// Returns: '/tmp/file'\n\npath.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');\n// If the current working directory is /home/myself/node,\n// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'\n
A TypeError is thrown if any of the arguments is not a string.
On Windows systems only, returns an equivalent namespace-prefixed path for\nthe given path. If path is not a string, path will be returned without\nmodifications.
This method is meaningful only on Windows systems. On POSIX systems, the\nmethod is non-operational and always returns path without modifications.
Provides the platform-specific path delimiter:
;
:
console.log(process.env.PATH);\n// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'\n\nprocess.env.PATH.split(path.delimiter);\n// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']\n
console.log(process.env.PATH);\n// Prints: 'C:\\Windows\\system32;C:\\Windows;C:\\Program Files\\node\\'\n\nprocess.env.PATH.split(path.delimiter);\n// Returns ['C:\\\\Windows\\\\system32', 'C:\\\\Windows', 'C:\\\\Program Files\\\\node\\\\']\n
The path.posix property provides access to POSIX specific implementations\nof the path methods.
The API is accessible via require('node:path').posix or require('node:path/posix').
require('node:path').posix
require('node:path/posix')
Provides the platform-specific path segment separator:
'foo/bar/baz'.split(path.sep);\n// Returns: ['foo', 'bar', 'baz']\n
'foo\\\\bar\\\\baz'.split(path.sep);\n// Returns: ['foo', 'bar', 'baz']\n
On Windows, both the forward slash (/) and backward slash (\\) are accepted\nas path segment separators; however, the path methods only add backward\nslashes (\\).
The path.win32 property provides access to Windows-specific implementations\nof the path methods.
The API is accessible via require('node:path').win32 or require('node:path/win32').
require('node:path').win32
require('node:path/win32')