2012-02-27 20:09:34 +01:00
|
|
|
# Path
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path` module provides utilities for working with file and directory paths.
|
|
|
|
It can be accessed using:
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
```js
|
|
|
|
const path = require('path');
|
|
|
|
```
|
|
|
|
|
|
|
|
## Windows vs. POSIX
|
|
|
|
|
|
|
|
The default operation of the `path` module varies based on the operating system
|
|
|
|
on which a Node.js application is running. Specifically, when running on a
|
|
|
|
Windows operating system, the `path` module will assume that Windows-style
|
|
|
|
paths are being used.
|
|
|
|
|
2018-10-25 05:16:24 +02:00
|
|
|
So using `path.basename()` might yield different results on POSIX and Windows:
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
On POSIX:
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.basename('C:\\temp\\myfile.html');
|
2016-12-06 06:34:25 +01:00
|
|
|
// Returns: 'C:\\temp\\myfile.html'
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
On Windows:
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.basename('C:\\temp\\myfile.html');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'myfile.html'
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
To achieve consistent results when working with Windows file paths on any
|
|
|
|
operating system, use [`path.win32`][]:
|
|
|
|
|
|
|
|
On POSIX and Windows:
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.win32.basename('C:\\temp\\myfile.html');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'myfile.html'
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
To achieve consistent results when working with POSIX file paths on any
|
|
|
|
operating system, use [`path.posix`][]:
|
|
|
|
|
|
|
|
On POSIX and Windows:
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.posix.basename('/tmp/myfile.html');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'myfile.html'
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-10-26 18:27:40 +02:00
|
|
|
On Windows Node.js follows the concept of per-drive working directory.
|
2017-06-06 11:19:23 +02:00
|
|
|
This behavior can be observed when using a drive path without a backslash. For
|
2018-08-26 18:02:27 +02:00
|
|
|
example, `path.resolve('c:\\')` can potentially return a different result than
|
2017-06-06 11:19:23 +02:00
|
|
|
`path.resolve('c:')`. For more information, see
|
|
|
|
[this MSDN page][MSDN-Rel-Path].
|
|
|
|
|
2016-02-21 09:04:09 +01:00
|
|
|
## path.basename(path[, ext])
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.25
|
2017-02-21 23:38:46 +01:00
|
|
|
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.
|
2016-05-26 03:48:32 +02:00
|
|
|
-->
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `path` {string}
|
|
|
|
* `ext` {string} An optional file extension
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
The `path.basename()` methods returns the last portion of a `path`, similar to
|
2017-04-03 13:02:12 +02:00
|
|
|
the Unix `basename` command. Trailing directory separators are ignored, see
|
|
|
|
[`path.sep`][].
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'quux.html'
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'quux'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
doc,test: empty strings in path module
The path module's `join, normalize, isAbsolute, relative and resolve`
functions return/use the current directory if they are passed zero
length strings.
> process.version
'v2.3.4-pre'
> path.win32.join('')
'.'
> path.posix.join('')
'.'
> path.win32.normalize('')
'.'
> path.posix.normalize('')
'.'
> path.win32.isAbsolute('')
false
> path.posix.isAbsolute('')
false
> path.win32.relative('', '')
''
> path.posix.relative('', '')
''
> path.win32relative('.', '')
''
> path.posix.relative('.', '')
''
> path.posix.resolve('')
'/home/thefourtheye/Desktop'
> path.win32.resolve('')
'\\home\\thefourtheye\\Desktop'
Since empty paths are not valid in any of the operating systems people
normally use, this behaviour might be a surprise to the users. This
commit introduces "Notes" about this, wherever applicable in `path`'s
documentation.
The tests makes sure that the behaviour is intact between
commits.
PR-URL: https://github.com/nodejs/io.js/pull/2106
Reviewed-By: Rich Trott <rtrott@gmail.com>
2015-07-05 17:08:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if `path` is not a string or if `ext` is given
|
|
|
|
and is not a string.
|
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.delimiter
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
* {string}
|
2016-11-08 19:00:30 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
Provides the platform-specific path delimiter:
|
|
|
|
|
|
|
|
* `;` for Windows
|
|
|
|
* `:` for POSIX
|
2013-03-13 18:27:09 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
For example, on POSIX:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
console.log(process.env.PATH);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
process.env.PATH.split(path.delimiter);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
On Windows:
|
2011-07-04 18:21:38 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
console.log(process.env.PATH);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
|
doc,test: empty strings in path module
The path module's `join, normalize, isAbsolute, relative and resolve`
functions return/use the current directory if they are passed zero
length strings.
> process.version
'v2.3.4-pre'
> path.win32.join('')
'.'
> path.posix.join('')
'.'
> path.win32.normalize('')
'.'
> path.posix.normalize('')
'.'
> path.win32.isAbsolute('')
false
> path.posix.isAbsolute('')
false
> path.win32.relative('', '')
''
> path.posix.relative('', '')
''
> path.win32relative('.', '')
''
> path.posix.relative('.', '')
''
> path.posix.resolve('')
'/home/thefourtheye/Desktop'
> path.win32.resolve('')
'\\home\\thefourtheye\\Desktop'
Since empty paths are not valid in any of the operating systems people
normally use, this behaviour might be a surprise to the users. This
commit introduces "Notes" about this, wherever applicable in `path`'s
documentation.
The tests makes sure that the behaviour is intact between
commits.
PR-URL: https://github.com/nodejs/io.js/pull/2106
Reviewed-By: Rich Trott <rtrott@gmail.com>
2015-07-05 17:08:16 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
process.env.PATH.split(path.delimiter);
|
|
|
|
// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-02-21 09:04:09 +01:00
|
|
|
## path.dirname(path)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.16
|
2017-02-21 23:38:46 +01:00
|
|
|
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.
|
2016-05-26 03:48:32 +02:00
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `path` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.dirname()` method returns the directory name of a `path`, similar to
|
2017-04-03 13:02:12 +02:00
|
|
|
the Unix `dirname` command. Trailing directory separators are ignored, see
|
|
|
|
[`path.sep`][].
|
2016-06-03 02:43:26 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.dirname('/foo/bar/baz/asdf/quux');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/foo/bar/baz/asdf'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if `path` is not a string.
|
|
|
|
|
2016-02-21 09:04:09 +01:00
|
|
|
## path.extname(path)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.25
|
2017-02-21 23:38:46 +01:00
|
|
|
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.
|
2016-05-26 03:48:32 +02:00
|
|
|
-->
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `path` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2016-02-21 09:04:09 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.extname()` method returns the extension of the `path`, from the last
|
2016-09-01 16:55:42 +02:00
|
|
|
occurrence of the `.` (period) character to end of string in the last portion of
|
2019-04-19 07:21:37 +02:00
|
|
|
the `path`. If there is no `.` in the last portion of the `path`, or if
|
|
|
|
there are no `.` characters other than the first character of
|
|
|
|
the basename of `path` (see `path.basename()`) , an empty string is returned.
|
2016-06-03 02:43:26 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.extname('index.html');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '.html'
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.extname('index.coffee.md');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '.md'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.extname('index.');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '.'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.extname('index');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: ''
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.extname('.index');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: ''
|
2019-04-19 07:21:37 +02:00
|
|
|
|
|
|
|
path.extname('.index.md');
|
|
|
|
// Returns: '.md'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if `path` is not a string.
|
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.format(pathObject)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.15
|
|
|
|
-->
|
2015-11-04 18:23:52 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `pathObject` {Object}
|
2017-02-04 16:15:33 +01:00
|
|
|
* `dir` {string}
|
|
|
|
* `root` {string}
|
|
|
|
* `base` {string}
|
|
|
|
* `name` {string}
|
|
|
|
* `ext` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2016-03-13 21:40:25 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.format()` method returns a path string from an object. This is the
|
|
|
|
opposite of [`path.parse()`][].
|
2016-03-13 21:40:25 +01:00
|
|
|
|
2016-12-01 18:54:46 +01:00
|
|
|
When providing properties to the `pathObject` remember that there are
|
|
|
|
combinations where one property has priority over another:
|
|
|
|
|
|
|
|
* `pathObject.root` is ignored if `pathObject.dir` is provided
|
|
|
|
* `pathObject.ext` and `pathObject.name` are ignored if `pathObject.base` exists
|
2016-03-13 21:40:25 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
For example, on POSIX:
|
2016-03-14 16:50:50 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-12-01 18:54:46 +01:00
|
|
|
// If `dir`, `root` and `base` are provided,
|
2016-06-03 02:43:26 +02:00
|
|
|
// `${dir}${path.sep}${base}`
|
2016-12-01 18:54:46 +01:00
|
|
|
// will be returned. `root` is ignored.
|
2016-01-17 18:39:07 +01:00
|
|
|
path.format({
|
2016-12-01 18:54:46 +01:00
|
|
|
root: '/ignored',
|
2016-06-03 02:43:26 +02:00
|
|
|
dir: '/home/user/dir',
|
|
|
|
base: 'file.txt'
|
2016-03-12 00:57:43 +01:00
|
|
|
});
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/home/user/dir/file.txt'
|
2016-01-17 18:39:07 +01:00
|
|
|
|
2016-03-22 02:20:16 +01:00
|
|
|
// `root` will be used if `dir` is not specified.
|
|
|
|
// If only `root` is provided or `dir` is equal to `root` then the
|
2016-12-01 18:54:46 +01:00
|
|
|
// platform separator will not be included. `ext` will be ignored.
|
2016-01-17 18:39:07 +01:00
|
|
|
path.format({
|
2016-06-03 02:43:26 +02:00
|
|
|
root: '/',
|
2016-12-01 18:54:46 +01:00
|
|
|
base: 'file.txt',
|
|
|
|
ext: 'ignored'
|
2016-03-22 02:20:16 +01:00
|
|
|
});
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/file.txt'
|
2015-08-17 05:26:46 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
// `name` + `ext` will be used if `base` is not specified.
|
2016-03-22 02:20:16 +01:00
|
|
|
path.format({
|
2016-06-03 02:43:26 +02:00
|
|
|
root: '/',
|
|
|
|
name: 'file',
|
|
|
|
ext: '.txt'
|
2016-03-22 02:20:16 +01:00
|
|
|
});
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/file.txt'
|
2016-03-22 02:20:16 +01:00
|
|
|
```
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
On Windows:
|
2016-03-14 16:50:50 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
path.format({
|
2017-04-10 01:45:33 +02:00
|
|
|
dir: 'C:\\path\\dir',
|
|
|
|
base: 'file.txt'
|
2016-07-15 07:41:29 +02:00
|
|
|
});
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'C:\\path\\dir\\file.txt'
|
2016-03-14 16:50:50 +01:00
|
|
|
```
|
|
|
|
|
2013-04-14 21:44:40 +02:00
|
|
|
## path.isAbsolute(path)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.2
|
|
|
|
-->
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `path` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {boolean}
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
The `path.isAbsolute()` method determines if `path` is an absolute path.
|
|
|
|
|
|
|
|
If the given `path` is a zero-length string, `false` will be returned.
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
For example, on POSIX:
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.isAbsolute('/foo/bar'); // true
|
|
|
|
path.isAbsolute('/baz/..'); // true
|
|
|
|
path.isAbsolute('qux/'); // false
|
|
|
|
path.isAbsolute('.'); // false
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
On Windows:
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.isAbsolute('//server'); // true
|
|
|
|
path.isAbsolute('\\\\server'); // true
|
|
|
|
path.isAbsolute('C:/foo/..'); // true
|
|
|
|
path.isAbsolute('C:\\foo\\..'); // true
|
|
|
|
path.isAbsolute('bar\\baz'); // false
|
|
|
|
path.isAbsolute('bar/baz'); // false
|
|
|
|
path.isAbsolute('.'); // false
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if `path` is not a string.
|
doc,test: empty strings in path module
The path module's `join, normalize, isAbsolute, relative and resolve`
functions return/use the current directory if they are passed zero
length strings.
> process.version
'v2.3.4-pre'
> path.win32.join('')
'.'
> path.posix.join('')
'.'
> path.win32.normalize('')
'.'
> path.posix.normalize('')
'.'
> path.win32.isAbsolute('')
false
> path.posix.isAbsolute('')
false
> path.win32.relative('', '')
''
> path.posix.relative('', '')
''
> path.win32relative('.', '')
''
> path.posix.relative('.', '')
''
> path.posix.resolve('')
'/home/thefourtheye/Desktop'
> path.win32.resolve('')
'\\home\\thefourtheye\\Desktop'
Since empty paths are not valid in any of the operating systems people
normally use, this behaviour might be a surprise to the users. This
commit introduces "Notes" about this, wherever applicable in `path`'s
documentation.
The tests makes sure that the behaviour is intact between
commits.
PR-URL: https://github.com/nodejs/io.js/pull/2106
Reviewed-By: Rich Trott <rtrott@gmail.com>
2015-07-05 17:08:16 +02:00
|
|
|
|
2016-08-30 07:35:03 +02:00
|
|
|
## path.join([...paths])
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.16
|
|
|
|
-->
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `...paths` {string} A sequence of path segments
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-09-14 03:06:30 +02:00
|
|
|
The `path.join()` method joins all given `path` segments together using the
|
2018-11-06 06:40:46 +01:00
|
|
|
platform-specific separator as a delimiter, then normalizes the resulting path.
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
Zero-length `path` segments are ignored. If the joined path string is a
|
|
|
|
zero-length string then `'.'` will be returned, representing the current
|
|
|
|
working directory.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/foo/bar/baz/asdf'
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.join('foo', {}, 'bar');
|
2018-12-10 13:27:32 +01:00
|
|
|
// Throws 'TypeError: Path must be a string. Received {}'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if any of the path segments is not a string.
|
doc,test: empty strings in path module
The path module's `join, normalize, isAbsolute, relative and resolve`
functions return/use the current directory if they are passed zero
length strings.
> process.version
'v2.3.4-pre'
> path.win32.join('')
'.'
> path.posix.join('')
'.'
> path.win32.normalize('')
'.'
> path.posix.normalize('')
'.'
> path.win32.isAbsolute('')
false
> path.posix.isAbsolute('')
false
> path.win32.relative('', '')
''
> path.posix.relative('', '')
''
> path.win32relative('.', '')
''
> path.posix.relative('.', '')
''
> path.posix.resolve('')
'/home/thefourtheye/Desktop'
> path.win32.resolve('')
'\\home\\thefourtheye\\Desktop'
Since empty paths are not valid in any of the operating systems people
normally use, this behaviour might be a surprise to the users. This
commit introduces "Notes" about this, wherever applicable in `path`'s
documentation.
The tests makes sure that the behaviour is intact between
commits.
PR-URL: https://github.com/nodejs/io.js/pull/2106
Reviewed-By: Rich Trott <rtrott@gmail.com>
2015-07-05 17:08:16 +02:00
|
|
|
|
2016-02-21 09:04:09 +01:00
|
|
|
## path.normalize(path)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.23
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `path` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
The `path.normalize()` method normalizes the given `path`, resolving `'..'` and
|
|
|
|
`'.'` segments.
|
2015-11-04 18:23:52 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
When multiple, sequential path segment separation characters are found (e.g.
|
2017-04-27 18:22:03 +02:00
|
|
|
`/` on POSIX and either `\` or `/` on Windows), they are replaced by a single
|
2018-11-06 06:40:46 +01:00
|
|
|
instance of the platform-specific path segment separator (`/` on POSIX and
|
2017-04-27 18:22:03 +02:00
|
|
|
`\` on Windows). Trailing separators are preserved.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
If the `path` is a zero-length string, `'.'` is returned, representing the
|
|
|
|
current working directory.
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
For example, on POSIX:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.normalize('/foo/bar//baz/asdf/quux/..');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/foo/bar/baz/asdf'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
On Windows:
|
|
|
|
|
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.normalize('C:\\temp\\\\foo\\bar\\..\\');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: 'C:\\temp\\foo\\'
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
|
|
|
|
2017-04-27 18:22:03 +02:00
|
|
|
Since Windows recognizes multiple path separators, both separators will be
|
|
|
|
replaced by instances of the Windows preferred separator (`\`):
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
|
|
|
|
// Returns: 'C:\\temp\\foo\\bar'
|
|
|
|
```
|
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if `path` is not a string.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-02-21 09:04:09 +01:00
|
|
|
## path.parse(path)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.15
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `path` {string}
|
2016-11-11 21:29:01 +01:00
|
|
|
* Returns: {Object}
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
The `path.parse()` method returns an object whose properties represent
|
2017-04-03 13:02:12 +02:00
|
|
|
significant elements of the `path`. Trailing directory separators are ignored,
|
|
|
|
see [`path.sep`][].
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
The returned object will have the following properties:
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `dir` {string}
|
2017-06-06 11:00:32 +02:00
|
|
|
* `root` {string}
|
2017-02-04 16:15:33 +01:00
|
|
|
* `base` {string}
|
|
|
|
* `name` {string}
|
2017-06-06 11:00:32 +02:00
|
|
|
* `ext` {string}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
For example, on POSIX:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.parse('/home/user/dir/file.txt');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns:
|
2017-04-10 01:45:33 +02:00
|
|
|
// { root: '/',
|
|
|
|
// dir: '/home/user/dir',
|
|
|
|
// base: 'file.txt',
|
|
|
|
// ext: '.txt',
|
|
|
|
// name: 'file' }
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
```text
|
|
|
|
┌─────────────────────┬────────────┐
|
|
|
|
│ dir │ base │
|
|
|
|
├──────┬ ├──────┬─────┤
|
|
|
|
│ root │ │ name │ ext │
|
|
|
|
" / home/user/dir / file .txt "
|
|
|
|
└──────┴──────────────┴──────┴─────┘
|
2018-04-02 07:38:48 +02:00
|
|
|
(all spaces in the "" line should be ignored — they are purely for formatting)
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
On Windows:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.parse('C:\\path\\dir\\file.txt');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns:
|
2017-04-10 01:45:33 +02:00
|
|
|
// { root: 'C:\\',
|
|
|
|
// dir: 'C:\\path\\dir',
|
|
|
|
// base: 'file.txt',
|
|
|
|
// ext: '.txt',
|
|
|
|
// name: 'file' }
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
```text
|
|
|
|
┌─────────────────────┬────────────┐
|
|
|
|
│ dir │ base │
|
|
|
|
├──────┬ ├──────┬─────┤
|
|
|
|
│ root │ │ name │ ext │
|
|
|
|
" C:\ path\dir \ file .txt "
|
|
|
|
└──────┴──────────────┴──────┴─────┘
|
2018-04-02 07:38:48 +02:00
|
|
|
(all spaces in the "" line should be ignored — they are purely for formatting)
|
2016-06-03 02:43:26 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
A [`TypeError`][] is thrown if `path` is not a string.
|
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.posix
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.15
|
|
|
|
-->
|
2014-02-01 16:10:25 +01:00
|
|
|
|
2016-11-08 19:00:30 +01:00
|
|
|
* {Object}
|
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.posix` property provides access to POSIX specific implementations
|
|
|
|
of the `path` methods.
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.relative(from, to)
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.0
|
2017-02-21 23:38:46 +01:00
|
|
|
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.
|
2016-05-26 03:48:32 +02:00
|
|
|
-->
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `from` {string}
|
|
|
|
* `to` {string}
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2016-06-03 02:43:26 +02:00
|
|
|
|
2017-06-16 05:19:16 +02:00
|
|
|
The `path.relative()` method returns the relative path from `from` to `to` based
|
|
|
|
on the current working directory. If `from` and `to` each resolve to the same
|
|
|
|
path (after calling `path.resolve()` on each), a zero-length string is returned.
|
2015-08-14 19:25:10 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
If a zero-length string is passed as `from` or `to`, the current working
|
|
|
|
directory will be used instead of the zero-length strings.
|
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
For example, on POSIX:
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '../../impl/bbb'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
On Windows:
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '..\\..\\impl\\bbb'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2017-06-25 21:31:54 +02:00
|
|
|
A [`TypeError`][] is thrown if either `from` or `to` is not a string.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-08-30 07:35:03 +02:00
|
|
|
## path.resolve([...paths])
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.4
|
|
|
|
-->
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `...paths` {string} A sequence of paths or path segments
|
2017-03-05 18:03:39 +01:00
|
|
|
* Returns: {string}
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.resolve()` method resolves a sequence of paths or path segments into
|
|
|
|
an absolute path.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The given sequence of paths is processed from right to left, with each
|
|
|
|
subsequent `path` prepended until an absolute path is constructed.
|
|
|
|
For instance, given the sequence of path segments: `/foo`, `/bar`, `baz`,
|
|
|
|
calling `path.resolve('/foo', '/bar', 'baz')` would return `/bar/baz`.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
If after processing all given `path` segments an absolute path has not yet
|
|
|
|
been generated, the current working directory is used.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The resulting path is normalized and trailing slashes are removed unless the
|
|
|
|
path is resolved to the root directory.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
Zero-length `path` segments are ignored.
|
2013-06-11 02:09:54 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
If no `path` segments are passed, `path.resolve()` will return the absolute path
|
|
|
|
of the current working directory.
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
path.resolve('/foo/bar', './baz');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/foo/bar/baz'
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.resolve('/foo/bar', '/tmp/file/');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: '/tmp/file'
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2017-04-21 16:38:31 +02:00
|
|
|
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
|
2018-12-10 13:27:32 +01:00
|
|
|
// If the current working directory is /home/myself/node,
|
2016-06-03 02:43:26 +02:00
|
|
|
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if any of the arguments is not a string.
|
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.sep
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.9
|
|
|
|
-->
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
* {string}
|
2016-11-08 19:00:30 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
Provides the platform-specific path segment separator:
|
|
|
|
|
|
|
|
* `\` on Windows
|
|
|
|
* `/` on POSIX
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
For example, on POSIX:
|
2015-11-04 18:23:52 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
'foo/bar/baz'.split(path.sep);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: ['foo', 'bar', 'baz']
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
On Windows:
|
2013-06-11 02:09:54 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-21 16:38:31 +02:00
|
|
|
'foo\\bar\\baz'.split(path.sep);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Returns: ['foo', 'bar', 'baz']
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2013-06-11 02:09:54 +02:00
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
On Windows, both the forward slash (`/`) and backward slash (`\`) are accepted
|
|
|
|
as path segment separators; however, the `path` methods only add backward
|
|
|
|
slashes (`\`).
|
2017-04-03 13:02:12 +02:00
|
|
|
|
2017-08-21 07:44:47 +02:00
|
|
|
## path.toNamespacedPath(path)
|
|
|
|
<!-- YAML
|
2017-09-01 18:50:47 +02:00
|
|
|
added: v9.0.0
|
2017-08-21 07:44:47 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `path` {string}
|
|
|
|
* Returns: {string}
|
|
|
|
|
|
|
|
On Windows systems only, returns an equivalent [namespace-prefixed path][] for
|
|
|
|
the given `path`. If `path` is not a string, `path` will be returned without
|
|
|
|
modifications.
|
|
|
|
|
2018-11-06 06:40:46 +01:00
|
|
|
This method is meaningful only on Windows system. On POSIX systems, the
|
2017-08-21 07:44:47 +02:00
|
|
|
method is non-operational and always returns `path` without modifications.
|
|
|
|
|
2013-06-11 02:09:54 +02:00
|
|
|
## path.win32
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.15
|
|
|
|
-->
|
2013-06-11 02:09:54 +02:00
|
|
|
|
2016-11-08 19:00:30 +01:00
|
|
|
* {Object}
|
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.win32` property provides access to Windows-specific implementations
|
|
|
|
of the `path` methods.
|
2016-01-05 11:49:54 +01:00
|
|
|
|
2017-05-08 18:30:13 +02:00
|
|
|
[`TypeError`]: errors.html#errors_class_typeerror
|
|
|
|
[`path.parse()`]: #path_path_parse_path
|
2016-06-03 02:43:26 +02:00
|
|
|
[`path.posix`]: #path_path_posix
|
2017-04-03 13:02:12 +02:00
|
|
|
[`path.sep`]: #path_path_sep
|
2016-06-03 02:43:26 +02:00
|
|
|
[`path.win32`]: #path_path_win32
|
2018-07-01 23:38:05 +02:00
|
|
|
[MSDN-Rel-Path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths
|
2018-07-14 14:10:10 +02:00
|
|
|
[namespace-prefixed path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces
|