11cb0ef41Sopenharmony_ci# Path
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/path.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:path` module provides utilities for working with file and directory
101cb0ef41Sopenharmony_cipaths. It can be accessed using:
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci```js
131cb0ef41Sopenharmony_ciconst path = require('node:path');
141cb0ef41Sopenharmony_ci```
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci## Windows vs. POSIX
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciThe default operation of the `node:path` module varies based on the operating
191cb0ef41Sopenharmony_cisystem on which a Node.js application is running. Specifically, when running on
201cb0ef41Sopenharmony_cia Windows operating system, the `node:path` module will assume that
211cb0ef41Sopenharmony_ciWindows-style paths are being used.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciSo using `path.basename()` might yield different results on POSIX and Windows:
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciOn POSIX:
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci```js
281cb0ef41Sopenharmony_cipath.basename('C:\\temp\\myfile.html');
291cb0ef41Sopenharmony_ci// Returns: 'C:\\temp\\myfile.html'
301cb0ef41Sopenharmony_ci```
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciOn Windows:
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci```js
351cb0ef41Sopenharmony_cipath.basename('C:\\temp\\myfile.html');
361cb0ef41Sopenharmony_ci// Returns: 'myfile.html'
371cb0ef41Sopenharmony_ci```
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciTo achieve consistent results when working with Windows file paths on any
401cb0ef41Sopenharmony_cioperating system, use [`path.win32`][]:
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciOn POSIX and Windows:
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci```js
451cb0ef41Sopenharmony_cipath.win32.basename('C:\\temp\\myfile.html');
461cb0ef41Sopenharmony_ci// Returns: 'myfile.html'
471cb0ef41Sopenharmony_ci```
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciTo achieve consistent results when working with POSIX file paths on any
501cb0ef41Sopenharmony_cioperating system, use [`path.posix`][]:
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciOn POSIX and Windows:
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci```js
551cb0ef41Sopenharmony_cipath.posix.basename('/tmp/myfile.html');
561cb0ef41Sopenharmony_ci// Returns: 'myfile.html'
571cb0ef41Sopenharmony_ci```
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciOn Windows Node.js follows the concept of per-drive working directory.
601cb0ef41Sopenharmony_ciThis behavior can be observed when using a drive path without a backslash. For
611cb0ef41Sopenharmony_ciexample, `path.resolve('C:\\')` can potentially return a different result than
621cb0ef41Sopenharmony_ci`path.resolve('C:')`. For more information, see
631cb0ef41Sopenharmony_ci[this MSDN page][MSDN-Rel-Path].
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci## `path.basename(path[, suffix])`
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci<!-- YAML
681cb0ef41Sopenharmony_ciadded: v0.1.25
691cb0ef41Sopenharmony_cichanges:
701cb0ef41Sopenharmony_ci  - version: v6.0.0
711cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5348
721cb0ef41Sopenharmony_ci    description: Passing a non-string as the `path` argument will throw now.
731cb0ef41Sopenharmony_ci-->
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci* `path` {string}
761cb0ef41Sopenharmony_ci* `suffix` {string} An optional suffix to remove
771cb0ef41Sopenharmony_ci* Returns: {string}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciThe `path.basename()` method returns the last portion of a `path`, similar to
801cb0ef41Sopenharmony_cithe Unix `basename` command. Trailing [directory separators][`path.sep`] are
811cb0ef41Sopenharmony_ciignored.
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci```js
841cb0ef41Sopenharmony_cipath.basename('/foo/bar/baz/asdf/quux.html');
851cb0ef41Sopenharmony_ci// Returns: 'quux.html'
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_cipath.basename('/foo/bar/baz/asdf/quux.html', '.html');
881cb0ef41Sopenharmony_ci// Returns: 'quux'
891cb0ef41Sopenharmony_ci```
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciAlthough Windows usually treats file names, including file extensions, in a
921cb0ef41Sopenharmony_cicase-insensitive manner, this function does not. For example, `C:\\foo.html` and
931cb0ef41Sopenharmony_ci`C:\\foo.HTML` refer to the same file, but `basename` treats the extension as a
941cb0ef41Sopenharmony_cicase-sensitive string:
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci```js
971cb0ef41Sopenharmony_cipath.win32.basename('C:\\foo.html', '.html');
981cb0ef41Sopenharmony_ci// Returns: 'foo'
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_cipath.win32.basename('C:\\foo.HTML', '.html');
1011cb0ef41Sopenharmony_ci// Returns: 'foo.HTML'
1021cb0ef41Sopenharmony_ci```
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if `path` is not a string or if `suffix` is given
1051cb0ef41Sopenharmony_ciand is not a string.
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci## `path.delimiter`
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci<!-- YAML
1101cb0ef41Sopenharmony_ciadded: v0.9.3
1111cb0ef41Sopenharmony_ci-->
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci* {string}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ciProvides the platform-specific path delimiter:
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci* `;` for Windows
1181cb0ef41Sopenharmony_ci* `:` for POSIX
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ciFor example, on POSIX:
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci```js
1231cb0ef41Sopenharmony_ciconsole.log(process.env.PATH);
1241cb0ef41Sopenharmony_ci// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciprocess.env.PATH.split(path.delimiter);
1271cb0ef41Sopenharmony_ci// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
1281cb0ef41Sopenharmony_ci```
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ciOn Windows:
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci```js
1331cb0ef41Sopenharmony_ciconsole.log(process.env.PATH);
1341cb0ef41Sopenharmony_ci// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciprocess.env.PATH.split(path.delimiter);
1371cb0ef41Sopenharmony_ci// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
1381cb0ef41Sopenharmony_ci```
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci## `path.dirname(path)`
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci<!-- YAML
1431cb0ef41Sopenharmony_ciadded: v0.1.16
1441cb0ef41Sopenharmony_cichanges:
1451cb0ef41Sopenharmony_ci  - version: v6.0.0
1461cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5348
1471cb0ef41Sopenharmony_ci    description: Passing a non-string as the `path` argument will throw now.
1481cb0ef41Sopenharmony_ci-->
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci* `path` {string}
1511cb0ef41Sopenharmony_ci* Returns: {string}
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ciThe `path.dirname()` method returns the directory name of a `path`, similar to
1541cb0ef41Sopenharmony_cithe Unix `dirname` command. Trailing directory separators are ignored, see
1551cb0ef41Sopenharmony_ci[`path.sep`][].
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci```js
1581cb0ef41Sopenharmony_cipath.dirname('/foo/bar/baz/asdf/quux');
1591cb0ef41Sopenharmony_ci// Returns: '/foo/bar/baz/asdf'
1601cb0ef41Sopenharmony_ci```
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if `path` is not a string.
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci## `path.extname(path)`
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci<!-- YAML
1671cb0ef41Sopenharmony_ciadded: v0.1.25
1681cb0ef41Sopenharmony_cichanges:
1691cb0ef41Sopenharmony_ci  - version: v6.0.0
1701cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5348
1711cb0ef41Sopenharmony_ci    description: Passing a non-string as the `path` argument will throw now.
1721cb0ef41Sopenharmony_ci-->
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci* `path` {string}
1751cb0ef41Sopenharmony_ci* Returns: {string}
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ciThe `path.extname()` method returns the extension of the `path`, from the last
1781cb0ef41Sopenharmony_cioccurrence of the `.` (period) character to end of string in the last portion of
1791cb0ef41Sopenharmony_cithe `path`. If there is no `.` in the last portion of the `path`, or if
1801cb0ef41Sopenharmony_cithere are no `.` characters other than the first character of
1811cb0ef41Sopenharmony_cithe basename of `path` (see `path.basename()`) , an empty string is returned.
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci```js
1841cb0ef41Sopenharmony_cipath.extname('index.html');
1851cb0ef41Sopenharmony_ci// Returns: '.html'
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_cipath.extname('index.coffee.md');
1881cb0ef41Sopenharmony_ci// Returns: '.md'
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_cipath.extname('index.');
1911cb0ef41Sopenharmony_ci// Returns: '.'
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_cipath.extname('index');
1941cb0ef41Sopenharmony_ci// Returns: ''
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_cipath.extname('.index');
1971cb0ef41Sopenharmony_ci// Returns: ''
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_cipath.extname('.index.md');
2001cb0ef41Sopenharmony_ci// Returns: '.md'
2011cb0ef41Sopenharmony_ci```
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if `path` is not a string.
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci## `path.format(pathObject)`
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci<!-- YAML
2081cb0ef41Sopenharmony_ciadded: v0.11.15
2091cb0ef41Sopenharmony_ci-->
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci* `pathObject` {Object} Any JavaScript object having the following properties:
2121cb0ef41Sopenharmony_ci  * `dir` {string}
2131cb0ef41Sopenharmony_ci  * `root` {string}
2141cb0ef41Sopenharmony_ci  * `base` {string}
2151cb0ef41Sopenharmony_ci  * `name` {string}
2161cb0ef41Sopenharmony_ci  * `ext` {string}
2171cb0ef41Sopenharmony_ci* Returns: {string}
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ciThe `path.format()` method returns a path string from an object. This is the
2201cb0ef41Sopenharmony_ciopposite of [`path.parse()`][].
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ciWhen providing properties to the `pathObject` remember that there are
2231cb0ef41Sopenharmony_cicombinations where one property has priority over another:
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci* `pathObject.root` is ignored if `pathObject.dir` is provided
2261cb0ef41Sopenharmony_ci* `pathObject.ext` and `pathObject.name` are ignored if `pathObject.base` exists
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ciFor example, on POSIX:
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci```js
2311cb0ef41Sopenharmony_ci// If `dir`, `root` and `base` are provided,
2321cb0ef41Sopenharmony_ci// `${dir}${path.sep}${base}`
2331cb0ef41Sopenharmony_ci// will be returned. `root` is ignored.
2341cb0ef41Sopenharmony_cipath.format({
2351cb0ef41Sopenharmony_ci  root: '/ignored',
2361cb0ef41Sopenharmony_ci  dir: '/home/user/dir',
2371cb0ef41Sopenharmony_ci  base: 'file.txt',
2381cb0ef41Sopenharmony_ci});
2391cb0ef41Sopenharmony_ci// Returns: '/home/user/dir/file.txt'
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci// `root` will be used if `dir` is not specified.
2421cb0ef41Sopenharmony_ci// If only `root` is provided or `dir` is equal to `root` then the
2431cb0ef41Sopenharmony_ci// platform separator will not be included. `ext` will be ignored.
2441cb0ef41Sopenharmony_cipath.format({
2451cb0ef41Sopenharmony_ci  root: '/',
2461cb0ef41Sopenharmony_ci  base: 'file.txt',
2471cb0ef41Sopenharmony_ci  ext: 'ignored',
2481cb0ef41Sopenharmony_ci});
2491cb0ef41Sopenharmony_ci// Returns: '/file.txt'
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci// `name` + `ext` will be used if `base` is not specified.
2521cb0ef41Sopenharmony_cipath.format({
2531cb0ef41Sopenharmony_ci  root: '/',
2541cb0ef41Sopenharmony_ci  name: 'file',
2551cb0ef41Sopenharmony_ci  ext: '.txt',
2561cb0ef41Sopenharmony_ci});
2571cb0ef41Sopenharmony_ci// Returns: '/file.txt'
2581cb0ef41Sopenharmony_ci```
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ciOn Windows:
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci```js
2631cb0ef41Sopenharmony_cipath.format({
2641cb0ef41Sopenharmony_ci  dir: 'C:\\path\\dir',
2651cb0ef41Sopenharmony_ci  base: 'file.txt',
2661cb0ef41Sopenharmony_ci});
2671cb0ef41Sopenharmony_ci// Returns: 'C:\\path\\dir\\file.txt'
2681cb0ef41Sopenharmony_ci```
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci## `path.isAbsolute(path)`
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci<!-- YAML
2731cb0ef41Sopenharmony_ciadded: v0.11.2
2741cb0ef41Sopenharmony_ci-->
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci* `path` {string}
2771cb0ef41Sopenharmony_ci* Returns: {boolean}
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ciThe `path.isAbsolute()` method determines if `path` is an absolute path.
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ciIf the given `path` is a zero-length string, `false` will be returned.
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ciFor example, on POSIX:
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci```js
2861cb0ef41Sopenharmony_cipath.isAbsolute('/foo/bar'); // true
2871cb0ef41Sopenharmony_cipath.isAbsolute('/baz/..');  // true
2881cb0ef41Sopenharmony_cipath.isAbsolute('qux/');     // false
2891cb0ef41Sopenharmony_cipath.isAbsolute('.');        // false
2901cb0ef41Sopenharmony_ci```
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ciOn Windows:
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci```js
2951cb0ef41Sopenharmony_cipath.isAbsolute('//server');    // true
2961cb0ef41Sopenharmony_cipath.isAbsolute('\\\\server');  // true
2971cb0ef41Sopenharmony_cipath.isAbsolute('C:/foo/..');   // true
2981cb0ef41Sopenharmony_cipath.isAbsolute('C:\\foo\\..'); // true
2991cb0ef41Sopenharmony_cipath.isAbsolute('bar\\baz');    // false
3001cb0ef41Sopenharmony_cipath.isAbsolute('bar/baz');     // false
3011cb0ef41Sopenharmony_cipath.isAbsolute('.');           // false
3021cb0ef41Sopenharmony_ci```
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if `path` is not a string.
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ci## `path.join([...paths])`
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci<!-- YAML
3091cb0ef41Sopenharmony_ciadded: v0.1.16
3101cb0ef41Sopenharmony_ci-->
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci* `...paths` {string} A sequence of path segments
3131cb0ef41Sopenharmony_ci* Returns: {string}
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ciThe `path.join()` method joins all given `path` segments together using the
3161cb0ef41Sopenharmony_ciplatform-specific separator as a delimiter, then normalizes the resulting path.
3171cb0ef41Sopenharmony_ci
3181cb0ef41Sopenharmony_ciZero-length `path` segments are ignored. If the joined path string is a
3191cb0ef41Sopenharmony_cizero-length string then `'.'` will be returned, representing the current
3201cb0ef41Sopenharmony_ciworking directory.
3211cb0ef41Sopenharmony_ci
3221cb0ef41Sopenharmony_ci```js
3231cb0ef41Sopenharmony_cipath.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
3241cb0ef41Sopenharmony_ci// Returns: '/foo/bar/baz/asdf'
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_cipath.join('foo', {}, 'bar');
3271cb0ef41Sopenharmony_ci// Throws 'TypeError: Path must be a string. Received {}'
3281cb0ef41Sopenharmony_ci```
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if any of the path segments is not a string.
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ci## `path.normalize(path)`
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci<!-- YAML
3351cb0ef41Sopenharmony_ciadded: v0.1.23
3361cb0ef41Sopenharmony_ci-->
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ci* `path` {string}
3391cb0ef41Sopenharmony_ci* Returns: {string}
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ciThe `path.normalize()` method normalizes the given `path`, resolving `'..'` and
3421cb0ef41Sopenharmony_ci`'.'` segments.
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ciWhen multiple, sequential path segment separation characters are found (e.g.
3451cb0ef41Sopenharmony_ci`/` on POSIX and either `\` or `/` on Windows), they are replaced by a single
3461cb0ef41Sopenharmony_ciinstance of the platform-specific path segment separator (`/` on POSIX and
3471cb0ef41Sopenharmony_ci`\` on Windows). Trailing separators are preserved.
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ciIf the `path` is a zero-length string, `'.'` is returned, representing the
3501cb0ef41Sopenharmony_cicurrent working directory.
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ciFor example, on POSIX:
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci```js
3551cb0ef41Sopenharmony_cipath.normalize('/foo/bar//baz/asdf/quux/..');
3561cb0ef41Sopenharmony_ci// Returns: '/foo/bar/baz/asdf'
3571cb0ef41Sopenharmony_ci```
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ciOn Windows:
3601cb0ef41Sopenharmony_ci
3611cb0ef41Sopenharmony_ci```js
3621cb0ef41Sopenharmony_cipath.normalize('C:\\temp\\\\foo\\bar\\..\\');
3631cb0ef41Sopenharmony_ci// Returns: 'C:\\temp\\foo\\'
3641cb0ef41Sopenharmony_ci```
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ciSince Windows recognizes multiple path separators, both separators will be
3671cb0ef41Sopenharmony_cireplaced by instances of the Windows preferred separator (`\`):
3681cb0ef41Sopenharmony_ci
3691cb0ef41Sopenharmony_ci```js
3701cb0ef41Sopenharmony_cipath.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
3711cb0ef41Sopenharmony_ci// Returns: 'C:\\temp\\foo\\bar'
3721cb0ef41Sopenharmony_ci```
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if `path` is not a string.
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ci## `path.parse(path)`
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci<!-- YAML
3791cb0ef41Sopenharmony_ciadded: v0.11.15
3801cb0ef41Sopenharmony_ci-->
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci* `path` {string}
3831cb0ef41Sopenharmony_ci* Returns: {Object}
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ciThe `path.parse()` method returns an object whose properties represent
3861cb0ef41Sopenharmony_cisignificant elements of the `path`. Trailing directory separators are ignored,
3871cb0ef41Sopenharmony_cisee [`path.sep`][].
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ciThe returned object will have the following properties:
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ci* `dir` {string}
3921cb0ef41Sopenharmony_ci* `root` {string}
3931cb0ef41Sopenharmony_ci* `base` {string}
3941cb0ef41Sopenharmony_ci* `name` {string}
3951cb0ef41Sopenharmony_ci* `ext` {string}
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ciFor example, on POSIX:
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ci```js
4001cb0ef41Sopenharmony_cipath.parse('/home/user/dir/file.txt');
4011cb0ef41Sopenharmony_ci// Returns:
4021cb0ef41Sopenharmony_ci// { root: '/',
4031cb0ef41Sopenharmony_ci//   dir: '/home/user/dir',
4041cb0ef41Sopenharmony_ci//   base: 'file.txt',
4051cb0ef41Sopenharmony_ci//   ext: '.txt',
4061cb0ef41Sopenharmony_ci//   name: 'file' }
4071cb0ef41Sopenharmony_ci```
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci```text
4101cb0ef41Sopenharmony_ci┌─────────────────────┬────────────┐
4111cb0ef41Sopenharmony_ci│          dir        │    base    │
4121cb0ef41Sopenharmony_ci├──────┬              ├──────┬─────┤
4131cb0ef41Sopenharmony_ci│ root │              │ name │ ext │
4141cb0ef41Sopenharmony_ci"  /    home/user/dir / file  .txt "
4151cb0ef41Sopenharmony_ci└──────┴──────────────┴──────┴─────┘
4161cb0ef41Sopenharmony_ci(All spaces in the "" line should be ignored. They are purely for formatting.)
4171cb0ef41Sopenharmony_ci```
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ciOn Windows:
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ci```js
4221cb0ef41Sopenharmony_cipath.parse('C:\\path\\dir\\file.txt');
4231cb0ef41Sopenharmony_ci// Returns:
4241cb0ef41Sopenharmony_ci// { root: 'C:\\',
4251cb0ef41Sopenharmony_ci//   dir: 'C:\\path\\dir',
4261cb0ef41Sopenharmony_ci//   base: 'file.txt',
4271cb0ef41Sopenharmony_ci//   ext: '.txt',
4281cb0ef41Sopenharmony_ci//   name: 'file' }
4291cb0ef41Sopenharmony_ci```
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci```text
4321cb0ef41Sopenharmony_ci┌─────────────────────┬────────────┐
4331cb0ef41Sopenharmony_ci│          dir        │    base    │
4341cb0ef41Sopenharmony_ci├──────┬              ├──────┬─────┤
4351cb0ef41Sopenharmony_ci│ root │              │ name │ ext │
4361cb0ef41Sopenharmony_ci" C:\      path\dir   \ file  .txt "
4371cb0ef41Sopenharmony_ci└──────┴──────────────┴──────┴─────┘
4381cb0ef41Sopenharmony_ci(All spaces in the "" line should be ignored. They are purely for formatting.)
4391cb0ef41Sopenharmony_ci```
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if `path` is not a string.
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ci## `path.posix`
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci<!-- YAML
4461cb0ef41Sopenharmony_ciadded: v0.11.15
4471cb0ef41Sopenharmony_cichanges:
4481cb0ef41Sopenharmony_ci  - version: v15.3.0
4491cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34962
4501cb0ef41Sopenharmony_ci    description: Exposed as `require('path/posix')`.
4511cb0ef41Sopenharmony_ci-->
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci* {Object}
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ciThe `path.posix` property provides access to POSIX specific implementations
4561cb0ef41Sopenharmony_ciof the `path` methods.
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ciThe API is accessible via `require('node:path').posix` or `require('node:path/posix')`.
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_ci## `path.relative(from, to)`
4611cb0ef41Sopenharmony_ci
4621cb0ef41Sopenharmony_ci<!-- YAML
4631cb0ef41Sopenharmony_ciadded: v0.5.0
4641cb0ef41Sopenharmony_cichanges:
4651cb0ef41Sopenharmony_ci  - version: v6.8.0
4661cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8523
4671cb0ef41Sopenharmony_ci    description: On Windows, the leading slashes for UNC paths are now included
4681cb0ef41Sopenharmony_ci                 in the return value.
4691cb0ef41Sopenharmony_ci-->
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_ci* `from` {string}
4721cb0ef41Sopenharmony_ci* `to` {string}
4731cb0ef41Sopenharmony_ci* Returns: {string}
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ciThe `path.relative()` method returns the relative path from `from` to `to` based
4761cb0ef41Sopenharmony_cion the current working directory. If `from` and `to` each resolve to the same
4771cb0ef41Sopenharmony_cipath (after calling `path.resolve()` on each), a zero-length string is returned.
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ciIf a zero-length string is passed as `from` or `to`, the current working
4801cb0ef41Sopenharmony_cidirectory will be used instead of the zero-length strings.
4811cb0ef41Sopenharmony_ci
4821cb0ef41Sopenharmony_ciFor example, on POSIX:
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_ci```js
4851cb0ef41Sopenharmony_cipath.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
4861cb0ef41Sopenharmony_ci// Returns: '../../impl/bbb'
4871cb0ef41Sopenharmony_ci```
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ciOn Windows:
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_ci```js
4921cb0ef41Sopenharmony_cipath.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
4931cb0ef41Sopenharmony_ci// Returns: '..\\..\\impl\\bbb'
4941cb0ef41Sopenharmony_ci```
4951cb0ef41Sopenharmony_ci
4961cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if either `from` or `to` is not a string.
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci## `path.resolve([...paths])`
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci<!-- YAML
5011cb0ef41Sopenharmony_ciadded: v0.3.4
5021cb0ef41Sopenharmony_ci-->
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_ci* `...paths` {string} A sequence of paths or path segments
5051cb0ef41Sopenharmony_ci* Returns: {string}
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ciThe `path.resolve()` method resolves a sequence of paths or path segments into
5081cb0ef41Sopenharmony_cian absolute path.
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_ciThe given sequence of paths is processed from right to left, with each
5111cb0ef41Sopenharmony_cisubsequent `path` prepended until an absolute path is constructed.
5121cb0ef41Sopenharmony_ciFor instance, given the sequence of path segments: `/foo`, `/bar`, `baz`,
5131cb0ef41Sopenharmony_cicalling `path.resolve('/foo', '/bar', 'baz')` would return `/bar/baz`
5141cb0ef41Sopenharmony_cibecause `'baz'` is not an absolute path but `'/bar' + '/' + 'baz'` is.
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ciIf, after processing all given `path` segments, an absolute path has not yet
5171cb0ef41Sopenharmony_cibeen generated, the current working directory is used.
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ciThe resulting path is normalized and trailing slashes are removed unless the
5201cb0ef41Sopenharmony_cipath is resolved to the root directory.
5211cb0ef41Sopenharmony_ci
5221cb0ef41Sopenharmony_ciZero-length `path` segments are ignored.
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ciIf no `path` segments are passed, `path.resolve()` will return the absolute path
5251cb0ef41Sopenharmony_ciof the current working directory.
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ci```js
5281cb0ef41Sopenharmony_cipath.resolve('/foo/bar', './baz');
5291cb0ef41Sopenharmony_ci// Returns: '/foo/bar/baz'
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_cipath.resolve('/foo/bar', '/tmp/file/');
5321cb0ef41Sopenharmony_ci// Returns: '/tmp/file'
5331cb0ef41Sopenharmony_ci
5341cb0ef41Sopenharmony_cipath.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
5351cb0ef41Sopenharmony_ci// If the current working directory is /home/myself/node,
5361cb0ef41Sopenharmony_ci// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'
5371cb0ef41Sopenharmony_ci```
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ciA [`TypeError`][] is thrown if any of the arguments is not a string.
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci## `path.sep`
5421cb0ef41Sopenharmony_ci
5431cb0ef41Sopenharmony_ci<!-- YAML
5441cb0ef41Sopenharmony_ciadded: v0.7.9
5451cb0ef41Sopenharmony_ci-->
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ci* {string}
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ciProvides the platform-specific path segment separator:
5501cb0ef41Sopenharmony_ci
5511cb0ef41Sopenharmony_ci* `\` on Windows
5521cb0ef41Sopenharmony_ci* `/` on POSIX
5531cb0ef41Sopenharmony_ci
5541cb0ef41Sopenharmony_ciFor example, on POSIX:
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ci```js
5571cb0ef41Sopenharmony_ci'foo/bar/baz'.split(path.sep);
5581cb0ef41Sopenharmony_ci// Returns: ['foo', 'bar', 'baz']
5591cb0ef41Sopenharmony_ci```
5601cb0ef41Sopenharmony_ci
5611cb0ef41Sopenharmony_ciOn Windows:
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ci```js
5641cb0ef41Sopenharmony_ci'foo\\bar\\baz'.split(path.sep);
5651cb0ef41Sopenharmony_ci// Returns: ['foo', 'bar', 'baz']
5661cb0ef41Sopenharmony_ci```
5671cb0ef41Sopenharmony_ci
5681cb0ef41Sopenharmony_ciOn Windows, both the forward slash (`/`) and backward slash (`\`) are accepted
5691cb0ef41Sopenharmony_cias path segment separators; however, the `path` methods only add backward
5701cb0ef41Sopenharmony_cislashes (`\`).
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ci## `path.toNamespacedPath(path)`
5731cb0ef41Sopenharmony_ci
5741cb0ef41Sopenharmony_ci<!-- YAML
5751cb0ef41Sopenharmony_ciadded: v9.0.0
5761cb0ef41Sopenharmony_ci-->
5771cb0ef41Sopenharmony_ci
5781cb0ef41Sopenharmony_ci* `path` {string}
5791cb0ef41Sopenharmony_ci* Returns: {string}
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_ciOn Windows systems only, returns an equivalent [namespace-prefixed path][] for
5821cb0ef41Sopenharmony_cithe given `path`. If `path` is not a string, `path` will be returned without
5831cb0ef41Sopenharmony_cimodifications.
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_ciThis method is meaningful only on Windows systems. On POSIX systems, the
5861cb0ef41Sopenharmony_cimethod is non-operational and always returns `path` without modifications.
5871cb0ef41Sopenharmony_ci
5881cb0ef41Sopenharmony_ci## `path.win32`
5891cb0ef41Sopenharmony_ci
5901cb0ef41Sopenharmony_ci<!-- YAML
5911cb0ef41Sopenharmony_ciadded: v0.11.15
5921cb0ef41Sopenharmony_cichanges:
5931cb0ef41Sopenharmony_ci  - version: v15.3.0
5941cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34962
5951cb0ef41Sopenharmony_ci    description: Exposed as `require('path/win32')`.
5961cb0ef41Sopenharmony_ci-->
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci* {Object}
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ciThe `path.win32` property provides access to Windows-specific implementations
6011cb0ef41Sopenharmony_ciof the `path` methods.
6021cb0ef41Sopenharmony_ci
6031cb0ef41Sopenharmony_ciThe API is accessible via `require('node:path').win32` or `require('node:path/win32')`.
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci[MSDN-Rel-Path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths
6061cb0ef41Sopenharmony_ci[`TypeError`]: errors.md#class-typeerror
6071cb0ef41Sopenharmony_ci[`path.parse()`]: #pathparsepath
6081cb0ef41Sopenharmony_ci[`path.posix`]: #pathposix
6091cb0ef41Sopenharmony_ci[`path.sep`]: #pathsep
6101cb0ef41Sopenharmony_ci[`path.win32`]: #pathwin32
6111cb0ef41Sopenharmony_ci[namespace-prefixed path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces
612