2012-02-27 20:09:34 +01:00
|
|
|
# Path
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01: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.
|
|
|
|
|
|
|
|
For example, using the `path.basename()` function with the Windows file path
|
|
|
|
`C:\temp\myfile.html`, will yield different results when running on POSIX than
|
|
|
|
when run on Windows:
|
|
|
|
|
|
|
|
On POSIX:
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.basename('C:\\temp\\myfile.html');
|
|
|
|
// returns 'C:\temp\myfile.html'
|
|
|
|
```
|
|
|
|
|
|
|
|
On Windows:
|
|
|
|
|
|
|
|
```js
|
|
|
|
path.basename('C:\\temp\\myfile.html');
|
|
|
|
// returns 'myfile.html'
|
|
|
|
```
|
|
|
|
|
|
|
|
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');
|
|
|
|
// returns 'myfile.html'
|
|
|
|
```
|
|
|
|
|
|
|
|
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');
|
|
|
|
// returns 'myfile.html'
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
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
|
|
|
|
-->
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `path` {String}
|
|
|
|
* `ext` {String} An optional file extension
|
|
|
|
|
|
|
|
The `path.basename()` methods returns the last portion of a `path`, similar to
|
|
|
|
the Unix `basename` command.
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
For example:
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html')
|
2016-06-03 02:43:26 +02:00
|
|
|
// returns 'quux.html'
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
|
2016-06-03 02:43:26 +02: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
|
|
|
|
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
|
|
|
|
console.log(process.env.PATH)
|
|
|
|
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
process.env.PATH.split(path.delimiter)
|
2016-03-12 00:57:43 +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
|
|
|
|
console.log(process.env.PATH)
|
|
|
|
// '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
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
process.env.PATH.split(path.delimiter)
|
2016-03-12 00:57:43 +01:00
|
|
|
// 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
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `path` {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
|
|
|
|
the Unix `dirname` command.
|
|
|
|
|
|
|
|
For example:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.dirname('/foo/bar/baz/asdf/quux')
|
2016-03-12 00:57:43 +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
|
|
|
|
-->
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `path` {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
|
|
|
|
occurance of the `.` (period) character to end of string in the last portion of
|
|
|
|
the `path`. If there is no `.` in the last portion of the `path`, or if the
|
|
|
|
first character of the basename of `path` (see `path.basename()`) is `.`, then
|
|
|
|
an empty string is returned.
|
|
|
|
|
|
|
|
For example:
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.extname('index.html')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '.html'
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('index.coffee.md')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '.md'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('index.')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '.'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('index')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns ''
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('.index')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns ''
|
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}
|
|
|
|
* `dir` {String}
|
|
|
|
* `root` {String}
|
|
|
|
* `base` {String}
|
|
|
|
* `name` {String}
|
|
|
|
* `ext` {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-06-03 02:43:26 +02:00
|
|
|
The following process is used when constructing the path string:
|
2016-03-13 21:40:25 +01:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `output` is set to an empty string.
|
|
|
|
* If `pathObject.dir` is specified, `pathObject.dir` is appended to `output`
|
|
|
|
followed by the value of `path.sep`;
|
|
|
|
* Otherwise, if `pathObject.root` is specified, `pathObject.root` is appended
|
|
|
|
to `output`.
|
|
|
|
* If `pathObject.base` is specified, `pathObject.base` is appended to `output`;
|
|
|
|
* Otherwise:
|
|
|
|
* If `pathObject.name` is specified, `pathObject.name` is appended to `output`
|
|
|
|
* If `pathObject.ext` is specified, `pathObject.ext` is appended to `output`.
|
|
|
|
* Return `output`
|
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-06-03 02:43:26 +02:00
|
|
|
// If `dir` and `base` are provided,
|
|
|
|
// `${dir}${path.sep}${base}`
|
2016-03-22 02:20:16 +01:00
|
|
|
// will be returned.
|
2016-01-17 18:39:07 +01:00
|
|
|
path.format({
|
2016-06-03 02:43:26 +02:00
|
|
|
dir: '/home/user/dir',
|
|
|
|
base: 'file.txt'
|
2016-03-12 00:57:43 +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
|
|
|
|
// platform separator will not be included.
|
2016-01-17 18:39:07 +01:00
|
|
|
path.format({
|
2016-06-03 02:43:26 +02:00
|
|
|
root: '/',
|
|
|
|
base: 'file.txt'
|
2016-03-22 02:20:16 +01:00
|
|
|
});
|
2016-03-12 00:57:43 +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
|
|
|
});
|
|
|
|
// returns '/file.txt'
|
|
|
|
|
|
|
|
// `base` will be returned if `dir` or `root` are not provided.
|
|
|
|
path.format({
|
2016-06-03 02:43:26 +02:00
|
|
|
base: 'file.txt'
|
2016-03-22 02:20:16 +01:00
|
|
|
});
|
|
|
|
// returns 'file.txt'
|
|
|
|
```
|
2016-06-03 02:43:26 +02:00
|
|
|
|
|
|
|
On Windows:
|
2016-03-14 16:50:50 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
path.format({
|
|
|
|
root : "C:\\",
|
|
|
|
dir : "C:\\path\\dir",
|
|
|
|
base : "file.txt",
|
|
|
|
ext : ".txt",
|
|
|
|
name : "file"
|
2016-07-15 07:41:29 +02:00
|
|
|
});
|
2016-03-14 16:50:50 +01:00
|
|
|
// returns 'C:\\path\\dir\\file.txt'
|
|
|
|
```
|
|
|
|
|
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
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `path` {String}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
For example on POSIX:
|
2013-04-14 21:44:40 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.isAbsolute('/foo/bar') // true
|
|
|
|
path.isAbsolute('/baz/..') // true
|
|
|
|
path.isAbsolute('qux/') // false
|
|
|
|
path.isAbsolute('.') // false
|
|
|
|
```
|
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
|
|
|
|
path.isAbsolute('//server') // true
|
|
|
|
path.isAbsolute('C:/foo/..') // true
|
|
|
|
path.isAbsolute('bar\\baz') // false
|
|
|
|
path.isAbsolute('.') // false
|
|
|
|
```
|
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-06-03 02:43:26 +02:00
|
|
|
## path.join([path[, ...]])
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.16
|
|
|
|
-->
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `[path[, ...]]` {String} A sequence of path segments
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
The `path.join()` method join all given `path` segments together using the
|
|
|
|
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.
|
|
|
|
|
|
|
|
For example:
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '/foo/bar/baz/asdf'
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.join('foo', {}, 'bar')
|
2016-06-03 02:43:26 +02:00
|
|
|
// throws TypeError: Arguments to path.join must be strings
|
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
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `path` {String}
|
|
|
|
|
|
|
|
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.
|
|
|
|
`/` on POSIX and `\` on Windows), they are replaced by a single instance of the
|
|
|
|
platform specific path segment separator. 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.
|
|
|
|
|
|
|
|
For example on POSIX:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.normalize('/foo/bar//baz/asdf/quux/..')
|
2016-03-12 00:57:43 +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
|
|
|
|
path.normalize('C:\\temp\\\\foo\\bar\\..\\');
|
|
|
|
// returns 'C:\\temp\\foo\\'
|
|
|
|
```
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `path` {String}
|
|
|
|
|
|
|
|
The `path.parse()` method returns an object whose properties represent
|
|
|
|
significant elements of the `path`.
|
|
|
|
|
|
|
|
The returned object will have the following properties:
|
|
|
|
|
|
|
|
* `root` {String}
|
|
|
|
* `dir` {String}
|
|
|
|
* `base` {String}
|
|
|
|
* `ext` {String}
|
|
|
|
* `name` {String}
|
2010-10-28 14:18:16 +02: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
|
|
|
|
path.parse('/home/user/dir/file.txt')
|
|
|
|
// returns
|
2016-03-12 00:57:43 +01: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 "
|
|
|
|
└──────┴──────────────┴──────┴─────┘
|
|
|
|
(all spaces in the "" line should be ignored -- they're purely for formatting)
|
|
|
|
```
|
|
|
|
|
|
|
|
On Windows:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-06-03 02:43:26 +02:00
|
|
|
path.parse('C:\\path\\dir\\file.txt')
|
2016-01-17 18:39:07 +01:00
|
|
|
// returns
|
2016-03-12 00:57:43 +01:00
|
|
|
// {
|
|
|
|
// root : "C:\\",
|
|
|
|
// dir : "C:\\path\\dir",
|
2016-06-03 02:43:26 +02:00
|
|
|
// base : "file.txt",
|
|
|
|
// ext : ".txt",
|
|
|
|
// name : "file"
|
2016-03-12 00:57:43 +01:00
|
|
|
// }
|
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 "
|
|
|
|
└──────┴──────────────┴──────┴─────┘
|
|
|
|
(all spaces in the "" line should be ignored -- they're purely for formatting)
|
|
|
|
```
|
|
|
|
|
|
|
|
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-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
|
|
|
|
-->
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `from` {String}
|
|
|
|
* `to` {String}
|
|
|
|
|
|
|
|
The `path.relative()` method returns the relative path from `from` to `to`.
|
|
|
|
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.
|
|
|
|
|
|
|
|
For example on POSIX:
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-06-03 02:43:26 +02:00
|
|
|
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
|
|
|
|
// 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
|
|
|
|
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '..\\..\\impl\\bbb'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
A [`TypeError`][] is thrown if neither `from` nor `to` is a string.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
## path.resolve([path[, ...]])
|
2016-05-26 03:48:32 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.4
|
|
|
|
-->
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
* `[path[, ...]]` {String} A sequence of paths or path segments
|
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-06-03 02:43:26 +02:00
|
|
|
For example:
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.resolve('/foo/bar', './baz')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '/foo/bar/baz'
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.resolve('/foo/bar', '/tmp/file/')
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns '/tmp/file'
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
|
2016-06-03 02:43:26 +02:00
|
|
|
// if the current working directory is /home/myself/node,
|
|
|
|
// 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
|
|
|
|
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
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
For example on POSIX:
|
2015-11-04 18:23:52 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
'foo/bar/baz'.split(path.sep)
|
2016-03-12 00:57:43 +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
|
|
|
|
'foo\\bar\\baz'.split(path.sep)
|
2016-03-12 00:57:43 +01:00
|
|
|
// returns ['foo', 'bar', 'baz']
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
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-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
|
|
|
|
2016-06-03 02:43:26 +02:00
|
|
|
[`path.posix`]: #path_path_posix
|
|
|
|
[`path.win32`]: #path_path_win32
|
|
|
|
[`path.parse()`]: #path_path_parse_path
|
|
|
|
[`TypeError`]: errors.html#errors_class_typeerror
|