{ "type": "module", "source": "doc/api/path.md", "modules": [ { "textRaw": "Path", "name": "path", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "
Source Code: lib/path.js
\nThe path
module provides utilities for working with file and directory paths.\nIt can be accessed using:
const path = require('path');\n
",
"modules": [
{
"textRaw": "Windows vs. POSIX",
"name": "windows_vs._posix",
"desc": "The default operation of the path
module varies based on the operating system\non which a Node.js application is running. Specifically, when running on a\nWindows operating system, the path
module will assume that Windows-style\npaths are being used.
So using path.basename()
might yield different results on POSIX and Windows:
On POSIX:
\npath.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'C:\\\\temp\\\\myfile.html'\n
\nOn Windows:
\npath.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'myfile.html'\n
\nTo achieve consistent results when working with Windows file paths on any\noperating system, use path.win32
:
On POSIX and Windows:
\npath.win32.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'myfile.html'\n
\nTo achieve consistent results when working with POSIX file paths on any\noperating system, use path.posix
:
On POSIX and Windows:
\npath.posix.basename('/tmp/myfile.html');\n// Returns: 'myfile.html'\n
\nOn 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.
The path.basename()
method returns the last portion of a path
, similar to\nthe Unix basename
command. Trailing directory separators are ignored, see\npath.sep
.
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
\nAlthough 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:
path.win32.basename('C:\\\\foo.html', '.html');\n// Returns: 'foo'\n\npath.win32.basename('C:\\\\foo.HTML', '.html');\n// Returns: 'foo.HTML'\n
\nA TypeError
is thrown if path
is not a string or if ext
is given\nand is not a string.
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('/foo/bar/baz/asdf/quux');\n// Returns: '/foo/bar/baz/asdf'\n
\nA 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('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
\nA TypeError
is thrown if path
is not a string.
The path.format()
method returns a path string from an object. This is the\nopposite of path.parse()
.
When providing properties to the pathObject
remember that there are\ncombinations where one property has priority over another:
pathObject.root
is ignored if pathObject.dir
is providedpathObject.ext
and pathObject.name
are ignored if pathObject.base
existsFor example, on POSIX:
\n// 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
\nOn Windows:
\npath.format({\n dir: 'C:\\\\path\\\\dir',\n base: 'file.txt'\n});\n// Returns: 'C:\\\\path\\\\dir\\\\file.txt'\n
"
},
{
"textRaw": "`path.isAbsolute(path)`",
"type": "method",
"name": "isAbsolute",
"meta": {
"added": [
"v0.11.2"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`path` {string}",
"name": "path",
"type": "string"
}
]
}
],
"desc": "The path.isAbsolute()
method determines if path
is an absolute path.
If the given path
is a zero-length string, false
will be returned.
For example, on POSIX:
\npath.isAbsolute('/foo/bar'); // true\npath.isAbsolute('/baz/..'); // true\npath.isAbsolute('qux/'); // false\npath.isAbsolute('.'); // false\n
\nOn Windows:
\npath.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
\nA TypeError
is thrown if path
is not a string.
The path.join()
method joins all given path
segments together using the\nplatform-specific separator as a delimiter, then normalizes the resulting path.
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
\nA 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.
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.
For example, on POSIX:
\npath.normalize('/foo/bar//baz/asdf/quux/..');\n// Returns: '/foo/bar/baz/asdf'\n
\nOn Windows:
\npath.normalize('C:\\\\temp\\\\\\\\foo\\\\bar\\\\..\\\\');\n// Returns: 'C:\\\\temp\\\\foo\\\\'\n
\nSince 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
\nA TypeError
is thrown if path
is not a string.
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:
\n\nFor example, on POSIX:
\npath.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┌─────────────────────┬────────────┐\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
\nOn Windows:
\npath.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┌─────────────────────┬────────────┐\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
\nA TypeError
is thrown if path
is not a string.
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.
If a zero-length string is passed as from
or to
, the current working\ndirectory will be used instead of the zero-length strings.
For example, on POSIX:
\npath.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');\n// Returns: '../../impl/bbb'\n
\nOn Windows:
\npath.relative('C:\\\\orandea\\\\test\\\\aaa', 'C:\\\\orandea\\\\impl\\\\bbb');\n// Returns: '..\\\\..\\\\impl\\\\bbb'\n
\nA 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.
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.
\nZero-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
\nA 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:
\n;
for Windows:
for POSIXFor example, on POSIX:
\nconsole.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
\nOn Windows:
\nconsole.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
"
},
{
"textRaw": "`posix` {Object}",
"type": "Object",
"name": "posix",
"meta": {
"added": [
"v0.11.15"
],
"changes": []
},
"desc": "The path.posix
property provides access to POSIX specific implementations\nof the path
methods.
Provides the platform-specific path segment separator:
\n\\
on Windows/
on POSIXFor example, on POSIX:
\n'foo/bar/baz'.split(path.sep);\n// Returns: ['foo', 'bar', 'baz']\n
\nOn Windows:
\n'foo\\\\bar\\\\baz'.split(path.sep);\n// Returns: ['foo', 'bar', 'baz']\n
\nOn 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.