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
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
This module contains utilities for handling and transforming file
|
|
|
|
paths. Almost all these methods perform only string transformations.
|
|
|
|
The file system is not consulted to check whether paths are valid.
|
|
|
|
|
|
|
|
Use `require('path')` to use this module. The following methods are provided:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.basename(p[, ext])
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Return the last portion of a path. Similar to the Unix `basename` command.
|
2011-01-06 06:39:00 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html')
|
|
|
|
// 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')
|
|
|
|
// returns
|
|
|
|
'quux'
|
|
|
|
```
|
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
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.delimiter
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
The platform-specific path delimiter, `;` or `':'`.
|
2013-03-13 18:27:09 +01:00
|
|
|
|
2016-01-21 22:55:55 +01:00
|
|
|
An example on \*nix:
|
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)
|
|
|
|
// returns
|
|
|
|
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
An example 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)
|
|
|
|
// returns
|
|
|
|
['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.dirname(p)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Return the directory name of a path. Similar to the Unix `dirname` command.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
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')
|
|
|
|
// returns
|
|
|
|
'/foo/bar/baz/asdf'
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.extname(p)
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Return the extension of the path, from the last '.' to end of string
|
|
|
|
in the last portion of the path. If there is no '.' in the last portion
|
|
|
|
of the path or the first character of it is '.', then it returns
|
|
|
|
an empty string. Examples:
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.extname('index.html')
|
|
|
|
// returns
|
|
|
|
'.html'
|
2011-01-11 02:57:25 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('index.coffee.md')
|
|
|
|
// returns
|
|
|
|
'.md'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('index.')
|
|
|
|
// returns
|
|
|
|
'.'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('index')
|
|
|
|
// returns
|
|
|
|
''
|
2011-01-06 06:39:00 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.extname('.index')
|
|
|
|
// returns
|
|
|
|
''
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.format(pathObject)
|
|
|
|
|
2016-01-05 11:49:54 +01:00
|
|
|
Returns a path string from an object, the opposite of [`path.parse`][].
|
2015-11-04 18:23:52 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.format({
|
|
|
|
root : "/",
|
|
|
|
dir : "/home/user/dir",
|
|
|
|
base : "file.txt",
|
|
|
|
ext : ".txt",
|
|
|
|
name : "file"
|
|
|
|
})
|
|
|
|
// returns
|
|
|
|
'/home/user/dir/file.txt'
|
|
|
|
|
|
|
|
// `root` will be used if `dir` is not specified and `name` + `ext` will be used
|
|
|
|
// if `base` is not specified
|
|
|
|
path.format({
|
|
|
|
root : "/",
|
|
|
|
ext : ".txt",
|
|
|
|
name : "file"
|
|
|
|
})
|
|
|
|
// returns
|
|
|
|
'/file.txt'
|
|
|
|
```
|
2015-08-17 05:26:46 +02:00
|
|
|
|
2013-04-14 21:44:40 +02:00
|
|
|
## path.isAbsolute(path)
|
|
|
|
|
|
|
|
Determines whether `path` is an absolute path. An absolute path will always
|
|
|
|
resolve to the same location, regardless of the working directory.
|
|
|
|
|
|
|
|
Posix examples:
|
|
|
|
|
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
|
|
|
|
|
|
|
Windows examples:
|
|
|
|
|
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
|
|
|
|
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
|
|
|
*Note:* If the path string passed as parameter is a zero-length string, unlike
|
|
|
|
other path module functions, it will be used as-is and `false` will be
|
|
|
|
returned.
|
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.join([path1][, path2][, ...])
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Join all arguments together and normalize the resulting path.
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Arguments must be strings. In v0.8, non-string arguments were
|
|
|
|
silently ignored. In v0.10 and up, an exception is thrown.
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
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', '..')
|
|
|
|
// 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')
|
|
|
|
// throws exception
|
|
|
|
TypeError: Arguments to path.join must be strings
|
|
|
|
```
|
2011-08-04 05:39:19 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
*Note:* If the arguments to `join` have zero-length strings, unlike other path
|
|
|
|
module functions, they will be ignored. If the joined path string is a
|
|
|
|
zero-length string then `'.'` will be returned, which represents the
|
|
|
|
current working directory.
|
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
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.normalize(p)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Normalize a string path, taking care of `'..'` and `'.'` parts.
|
|
|
|
|
|
|
|
When multiple slashes are found, they're replaced by a single one;
|
|
|
|
when the path contains a trailing slash, it is preserved.
|
|
|
|
On Windows backslashes are used.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.normalize('/foo/bar//baz/asdf/quux/..')
|
|
|
|
// returns
|
|
|
|
'/foo/bar/baz/asdf'
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
*Note:* If the path string passed as argument is a zero-length string then `'.'`
|
|
|
|
will be returned, which represents the current working directory.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.parse(pathString)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Returns an object from a path string.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-21 22:55:55 +01:00
|
|
|
An example on \*nix:
|
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
|
|
|
|
{
|
|
|
|
root : "/",
|
|
|
|
dir : "/home/user/dir",
|
|
|
|
base : "file.txt",
|
|
|
|
ext : ".txt",
|
|
|
|
name : "file"
|
|
|
|
}
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
An example on Windows:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.parse('C:\\path\\dir\\index.html')
|
|
|
|
// returns
|
|
|
|
{
|
|
|
|
root : "C:\\",
|
|
|
|
dir : "C:\\path\\dir",
|
|
|
|
base : "index.html",
|
|
|
|
ext : ".html",
|
|
|
|
name : "index"
|
|
|
|
}
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.posix
|
2014-02-01 16:10:25 +01:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Provide access to aforementioned `path` methods but always interact in a posix
|
|
|
|
compatible way.
|
2011-08-31 15:12:34 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.relative(from, to)
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Solve the relative path from `from` to `to`.
|
2015-08-14 19:25:10 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
At times we have two absolute paths, and we need to derive the relative
|
|
|
|
path from one to the other. This is actually the reverse transform of
|
|
|
|
`path.resolve`, which means we see that:
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.resolve(from, path.relative(from, to)) == path.resolve(to)
|
|
|
|
```
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Examples:
|
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')
|
|
|
|
// returns
|
|
|
|
'..\\..\\impl\\bbb'
|
2012-04-02 18:31:21 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
|
|
|
|
// returns
|
|
|
|
'../../impl/bbb'
|
|
|
|
```
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
*Note:* If the arguments to `relative` have zero-length strings then the current
|
|
|
|
working directory will be used instead of the zero-length strings. If
|
|
|
|
both the paths are the same then a zero-length string will be returned.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.resolve([from ...], to)
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Resolves `to` to an absolute path.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
If `to` isn't already absolute `from` arguments are prepended in right to left
|
|
|
|
order, until an absolute path is found. If after using all `from` paths still
|
|
|
|
no absolute path is found, the current working directory is used as well. The
|
|
|
|
resulting path is normalized, and trailing slashes are removed unless the path
|
|
|
|
gets resolved to the root directory. Non-string `from` arguments are ignored.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Another way to think of it is as a sequence of `cd` commands in a shell.
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
|
|
|
|
```
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Is similar to:
|
2012-10-01 22:10:32 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
cd foo/bar
|
|
|
|
cd /tmp/file/
|
|
|
|
cd ..
|
|
|
|
cd a/../subfile
|
|
|
|
pwd
|
|
|
|
```
|
2013-06-11 02:09:54 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
The difference is that the different paths don't need to exist and may also be
|
|
|
|
files.
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
Examples:
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
path.resolve('/foo/bar', './baz')
|
|
|
|
// 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/')
|
|
|
|
// 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')
|
|
|
|
// if currently in /home/myself/node, it returns
|
|
|
|
'/home/myself/node/wwwroot/static_files/gif/image.gif'
|
|
|
|
```
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
*Note:* If the arguments to `resolve` have zero-length strings then the current
|
|
|
|
working directory will be used instead of them.
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
## path.sep
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
The platform-specific file separator. `'\\'` or `'/'`.
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2016-01-21 22:55:55 +01:00
|
|
|
An example on \*nix:
|
2015-11-04 18:23:52 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
'foo/bar/baz'.split(path.sep)
|
|
|
|
// returns
|
|
|
|
['foo', 'bar', 'baz']
|
|
|
|
```
|
2014-04-08 23:48:24 +02:00
|
|
|
|
2015-11-04 18:23:52 +01:00
|
|
|
An example 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)
|
|
|
|
// returns
|
|
|
|
['foo', 'bar', 'baz']
|
|
|
|
```
|
2013-06-11 02:09:54 +02:00
|
|
|
|
|
|
|
## path.win32
|
|
|
|
|
|
|
|
Provide access to aforementioned `path` methods but always interact in a win32
|
|
|
|
compatible way.
|
2016-01-05 11:49:54 +01:00
|
|
|
|
|
|
|
[`path.parse`]: #path_path_parse_pathstring
|