{ "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

\n

The path module provides utilities for working with file and directory paths.\nIt can be accessed using:

\n
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.

\n

So using path.basename() might yield different results on POSIX and Windows:

\n

On POSIX:

\n
path.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'C:\\\\temp\\\\myfile.html'\n
\n

On Windows:

\n
path.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'myfile.html'\n
\n

To achieve consistent results when working with Windows file paths on any\noperating system, use path.win32:

\n

On POSIX and Windows:

\n
path.win32.basename('C:\\\\temp\\\\myfile.html');\n// Returns: 'myfile.html'\n
\n

To achieve consistent results when working with POSIX file paths on any\noperating system, use path.posix:

\n

On POSIX and Windows:

\n
path.posix.basename('/tmp/myfile.html');\n// Returns: 'myfile.html'\n
\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.

", "type": "module", "displayName": "Windows vs. POSIX" } ], "methods": [ { "textRaw": "`path.basename(path[, ext])`", "type": "method", "name": "basename", "meta": { "added": [ "v0.1.25" ], "changes": [ { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/5348", "description": "Passing a non-string as the `path` argument will throw now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" }, { "textRaw": "`ext` {string} An optional file extension", "name": "ext", "type": "string", "desc": "An optional file extension" } ] } ], "desc": "

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.

\n
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
\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:

\n
path.win32.basename('C:\\\\foo.html', '.html');\n// Returns: 'foo'\n\npath.win32.basename('C:\\\\foo.HTML', '.html');\n// Returns: 'foo.HTML'\n
\n

A TypeError is thrown if path is not a string or if ext is given\nand is not a string.

" }, { "textRaw": "`path.dirname(path)`", "type": "method", "name": "dirname", "meta": { "added": [ "v0.1.16" ], "changes": [ { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/5348", "description": "Passing a non-string as the `path` argument will throw now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" } ] } ], "desc": "

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.

\n
path.dirname('/foo/bar/baz/asdf/quux');\n// Returns: '/foo/bar/baz/asdf'\n
\n

A TypeError is thrown if path is not a string.

" }, { "textRaw": "`path.extname(path)`", "type": "method", "name": "extname", "meta": { "added": [ "v0.1.25" ], "changes": [ { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/5348", "description": "Passing a non-string as the `path` argument will throw now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" } ] } ], "desc": "

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.

\n
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
\n

A TypeError is thrown if path is not a string.

" }, { "textRaw": "`path.format(pathObject)`", "type": "method", "name": "format", "meta": { "added": [ "v0.11.15" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`pathObject` {Object}", "name": "pathObject", "type": "Object", "options": [ { "textRaw": "`dir` {string}", "name": "dir", "type": "string" }, { "textRaw": "`root` {string}", "name": "root", "type": "string" }, { "textRaw": "`base` {string}", "name": "base", "type": "string" }, { "textRaw": "`name` {string}", "name": "name", "type": "string" }, { "textRaw": "`ext` {string}", "name": "ext", "type": "string" } ] } ] } ], "desc": "

The path.format() method returns a path string from an object. This is the\nopposite of path.parse().

\n

When providing properties to the pathObject remember that there are\ncombinations where one property has priority over another:

\n\n

For 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
\n

On Windows:

\n
path.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.

\n

If the given path is a zero-length string, false will be returned.

\n

For example, on POSIX:

\n
path.isAbsolute('/foo/bar'); // true\npath.isAbsolute('/baz/..');  // true\npath.isAbsolute('qux/');     // false\npath.isAbsolute('.');        // false\n
\n

On Windows:

\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
\n

A TypeError is thrown if path is not a string.

" }, { "textRaw": "`path.join([...paths])`", "type": "method", "name": "join", "meta": { "added": [ "v0.1.16" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`...paths` {string} A sequence of path segments", "name": "...paths", "type": "string", "desc": "A sequence of path segments" } ] } ], "desc": "

The path.join() method joins all given path segments together using the\nplatform-specific separator as a delimiter, then normalizes the resulting path.

\n

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.

\n
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
\n

A TypeError is thrown if any of the path segments is not a string.

" }, { "textRaw": "`path.normalize(path)`", "type": "method", "name": "normalize", "meta": { "added": [ "v0.1.23" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" } ] } ], "desc": "

The path.normalize() method normalizes the given path, resolving '..' and\n'.' segments.

\n

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.

\n

If the path is a zero-length string, '.' is returned, representing the\ncurrent working directory.

\n

For example, on POSIX:

\n
path.normalize('/foo/bar//baz/asdf/quux/..');\n// Returns: '/foo/bar/baz/asdf'\n
\n

On Windows:

\n
path.normalize('C:\\\\temp\\\\\\\\foo\\\\bar\\\\..\\\\');\n// Returns: 'C:\\\\temp\\\\foo\\\\'\n
\n

Since Windows recognizes multiple path separators, both separators will be\nreplaced by instances of the Windows preferred separator (\\):

\n
path.win32.normalize('C:////temp\\\\\\\\/\\\\/\\\\/foo/bar');\n// Returns: 'C:\\\\temp\\\\foo\\\\bar'\n
\n

A TypeError is thrown if path is not a string.

" }, { "textRaw": "`path.parse(path)`", "type": "method", "name": "parse", "meta": { "added": [ "v0.11.15" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object}", "name": "return", "type": "Object" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" } ] } ], "desc": "

The path.parse() method returns an object whose properties represent\nsignificant elements of the path. Trailing directory separators are ignored,\nsee path.sep.

\n

The returned object will have the following properties:

\n\n

For example, on POSIX:

\n
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
┌─────────────────────┬────────────┐\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
\n

On Windows:

\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
┌─────────────────────┬────────────┐\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
\n

A TypeError is thrown if path is not a string.

" }, { "textRaw": "`path.relative(from, to)`", "type": "method", "name": "relative", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": "v6.8.0", "pr-url": "https://github.com/nodejs/node/pull/8523", "description": "On Windows, the leading slashes for UNC paths are now included in the return value." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`from` {string}", "name": "from", "type": "string" }, { "textRaw": "`to` {string}", "name": "to", "type": "string" } ] } ], "desc": "

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.

\n

If a zero-length string is passed as from or to, the current working\ndirectory will be used instead of the zero-length strings.

\n

For example, on POSIX:

\n
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');\n// Returns: '../../impl/bbb'\n
\n

On Windows:

\n
path.relative('C:\\\\orandea\\\\test\\\\aaa', 'C:\\\\orandea\\\\impl\\\\bbb');\n// Returns: '..\\\\..\\\\impl\\\\bbb'\n
\n

A TypeError is thrown if either from or to is not a string.

" }, { "textRaw": "`path.resolve([...paths])`", "type": "method", "name": "resolve", "meta": { "added": [ "v0.3.4" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`...paths` {string} A sequence of paths or path segments", "name": "...paths", "type": "string", "desc": "A sequence of paths or path segments" } ] } ], "desc": "

The path.resolve() method resolves a sequence of paths or path segments into\nan absolute path.

\n

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.

\n

If, after processing all given path segments, an absolute path has not yet\nbeen generated, the current working directory is used.

\n

The resulting path is normalized and trailing slashes are removed unless the\npath is resolved to the root directory.

\n

Zero-length path segments are ignored.

\n

If no path segments are passed, path.resolve() will return the absolute path\nof the current working directory.

\n
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
\n

A TypeError is thrown if any of the arguments is not a string.

" }, { "textRaw": "`path.toNamespacedPath(path)`", "type": "method", "name": "toNamespacedPath", "meta": { "added": [ "v9.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`path` {string}", "name": "path", "type": "string" } ] } ], "desc": "

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.

\n

This method is meaningful only on Windows systems. On POSIX systems, the\nmethod is non-operational and always returns path without modifications.

" } ], "properties": [ { "textRaw": "`delimiter` {string}", "type": "string", "name": "delimiter", "meta": { "added": [ "v0.9.3" ], "changes": [] }, "desc": "

Provides the platform-specific path delimiter:

\n\n

For example, on POSIX:

\n
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
\n

On Windows:

\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
" }, { "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.

" }, { "textRaw": "`sep` {string}", "type": "string", "name": "sep", "meta": { "added": [ "v0.7.9" ], "changes": [] }, "desc": "

Provides the platform-specific path segment separator:

\n\n

For example, on POSIX:

\n
'foo/bar/baz'.split(path.sep);\n// Returns: ['foo', 'bar', 'baz']\n
\n

On Windows:

\n
'foo\\\\bar\\\\baz'.split(path.sep);\n// Returns: ['foo', 'bar', 'baz']\n
\n

On Windows, both the forward slash (/) and backward slash (\\) are accepted\nas path segment separators; however, the path methods only add backward\nslashes (\\).

" }, { "textRaw": "`win32` {Object}", "type": "Object", "name": "win32", "meta": { "added": [ "v0.11.15" ], "changes": [] }, "desc": "

The path.win32 property provides access to Windows-specific implementations\nof the path methods.

" } ], "type": "module", "displayName": "Path" } ] }