{ "source": "doc/api/path.md", "modules": [ { "textRaw": "Path", "name": "path", "stability": 2, "stabilityText": "Stable", "desc": "
The path
module provides utilities for working with file and directory paths.\nIt can be accessed using:
const path = require('path');\n
\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.
For example, using the path.basename()
function with the Windows file path\nC:\\temp\\myfile.html
, will yield different results when running on POSIX than\nwhen run on 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
\n",
"type": "module",
"displayName": "Windows vs. POSIX"
}
],
"methods": [
{
"textRaw": "path.basename(path[, ext])",
"type": "method",
"name": "basename",
"meta": {
"added": [
"v0.1.25"
]
},
"signatures": [
{
"params": [
{
"textRaw": "`path` {String} ",
"name": "path",
"type": "String"
},
{
"textRaw": "`ext` {String} An optional file extension ",
"name": "ext",
"type": "String",
"desc": "An optional file extension",
"optional": true
}
]
},
{
"params": [
{
"name": "path"
},
{
"name": "ext",
"optional": true
}
]
}
],
"desc": "The path.basename()
methods returns the last portion of a path
, similar to\nthe Unix basename
command.
For example:
\npath.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
\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.
For example:
\npath.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 the\nfirst character of the basename of path
(see path.basename()
) is .
, then\nan empty string is returned.
For example:
\npath.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
\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()
.
The following process is used when constructing the path string:
\noutput
is set to an empty string.pathObject.dir
is specified, pathObject.dir
is appended to output
\nfollowed by the value of path.sep
;pathObject.root
is specified, pathObject.root
is appended\nto output
.pathObject.base
is specified, pathObject.base
is appended to output
;pathObject.name
is specified, pathObject.name
is appended to output
pathObject.ext
is specified, pathObject.ext
is appended to output
.output
For example, on POSIX:
\n// If `dir` and `base` are provided,\n// `${dir}${path.sep}${base}`\n// will be returned.\npath.format({\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.\npath.format({\n root: '/',\n base: 'file.txt'\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\n// `base` will be returned if `dir` or `root` are not provided.\npath.format({\n base: 'file.txt'\n});\n// returns 'file.txt'\n
\nOn Windows:
\npath.format({\n root : "C:\\\\",\n dir : "C:\\\\path\\\\dir",\n base : "file.txt",\n ext : ".txt",\n name : "file"\n});\n// returns 'C:\\\\path\\\\dir\\\\file.txt'\n
\n"
},
{
"textRaw": "path.isAbsolute(path)",
"type": "method",
"name": "isAbsolute",
"meta": {
"added": [
"v0.11.2"
]
},
"signatures": [
{
"params": [
{
"textRaw": "`path` {String} ",
"name": "path",
"type": "String"
}
]
},
{
"params": [
{
"name": "path"
}
]
}
],
"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.
For example:
\npath.join('/foo', 'bar', 'baz/asdf', 'quux', '..')\n// returns '/foo/bar/baz/asdf'\n\npath.join('foo', {}, 'bar')\n// throws TypeError: Arguments to path.join must be strings\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 \\
on Windows), they are replaced by a single instance of the\nplatform specific path segment separator. 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
\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
.
The returned object will have the following properties:
\nroot
{String}dir
{String}base
{String}ext
{String}name
{String}For example on POSIX:
\npath.parse('/home/user/dir/file.txt')\n// returns\n// {\n// root : "/",\n// dir : "/home/user/dir",\n// base : "file.txt",\n// ext : ".txt",\n// name : "file"\n// }\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// {\n// root : "C:\\\\",\n// dir : "C:\\\\path\\\\dir",\n// base : "file.txt",\n// ext : ".txt",\n// name : "file"\n// }\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
.\nIf from
and to
each resolve to the same path (after calling path.resolve()
\non 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 neither from
nor to
is 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
.
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.
For example:
\npath.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.
Provides the platform-specific path delimiter:
\n;
for Windows:
for POSIXFor example, on POSIX:
\nconsole.log(process.env.PATH)\n// '/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// '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
\n"
},
{
"textRaw": "path.posix",
"name": "posix",
"meta": {
"added": [
"v0.11.15"
]
},
"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
\n"
},
{
"textRaw": "path.win32",
"name": "win32",
"meta": {
"added": [
"v0.11.15"
]
},
"desc": "The path.win32
property provides access to Windows-specific implementations\nof the path
methods.
Note: On Windows, both the forward slash (/
) and backward slash (\\
)\ncharacters are accepted as path delimiters; however, only the backward slash\n(\\
) will be used in return values.