mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
8.4 KiB
357 lines
8.4 KiB
13 years ago
|
# Path
|
||
14 years ago
|
|
||
10 years ago
|
Stability: 2 - Stable
|
||
13 years ago
|
|
||
14 years ago
|
This module contains utilities for handling and transforming file
|
||
9 years ago
|
paths. The file system is not consulted to check whether paths are valid.
|
||
14 years ago
|
|
||
|
Use `require('path')` to use this module. The following methods are provided:
|
||
14 years ago
|
|
||
9 years ago
|
## path.basename(path[, ext])
|
||
14 years ago
|
|
||
9 years ago
|
Return the last portion of a path, similar to the Unix `basename` command.
|
||
|
`path` must be a string. `ext`, if given, must also be a string.
|
||
14 years ago
|
|
||
9 years ago
|
Examples:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.basename('/foo/bar/baz/asdf/quux.html')
|
||
9 years ago
|
// returns 'quux.html'
|
||
14 years ago
|
|
||
9 years ago
|
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
|
||
9 years ago
|
// returns 'quux'
|
||
9 years ago
|
```
|
||
10 years ago
|
|
||
9 years ago
|
## path.delimiter
|
||
14 years ago
|
|
||
9 years ago
|
The platform-specific path delimiter, `;` or `':'`.
|
||
12 years ago
|
|
||
9 years ago
|
An example on \*nix:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
console.log(process.env.PATH)
|
||
|
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
|
||
14 years ago
|
|
||
9 years ago
|
process.env.PATH.split(path.delimiter)
|
||
9 years ago
|
// returns ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
An example on Windows:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
console.log(process.env.PATH)
|
||
|
// 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
|
||
10 years ago
|
|
||
9 years ago
|
process.env.PATH.split(path.delimiter)
|
||
9 years ago
|
// returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
## path.dirname(path)
|
||
14 years ago
|
|
||
9 years ago
|
Return the directory name of a path, similar to the Unix `dirname` command.
|
||
|
`path` must be a string.
|
||
14 years ago
|
|
||
9 years ago
|
Example:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.dirname('/foo/bar/baz/asdf/quux')
|
||
9 years ago
|
// returns '/foo/bar/baz/asdf'
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
## path.extname(path)
|
||
14 years ago
|
|
||
9 years ago
|
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
|
||
9 years ago
|
an empty string. `path` must be a string.
|
||
|
|
||
|
Examples:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.extname('index.html')
|
||
9 years ago
|
// returns '.html'
|
||
14 years ago
|
|
||
9 years ago
|
path.extname('index.coffee.md')
|
||
9 years ago
|
// returns '.md'
|
||
14 years ago
|
|
||
9 years ago
|
path.extname('index.')
|
||
9 years ago
|
// returns '.'
|
||
14 years ago
|
|
||
9 years ago
|
path.extname('index')
|
||
9 years ago
|
// returns ''
|
||
14 years ago
|
|
||
9 years ago
|
path.extname('.index')
|
||
9 years ago
|
// returns ''
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
## path.format(pathObject)
|
||
|
|
||
9 years ago
|
Returns a path string from an object. This is the opposite of [`path.parse`][].
|
||
|
|
||
9 years ago
|
If `pathObject` has `dir` and `base` properties, the returned string will
|
||
|
be a concatenation of the `dir` property, the platform-dependent path separator,
|
||
|
and the `base` property.
|
||
9 years ago
|
|
||
|
If the `dir` property is not supplied, the `root` property will be used as the
|
||
|
`dir` property. However, it will be assumed that the `root` property already
|
||
|
ends with the platform-dependent path separator. In this case, the returned
|
||
9 years ago
|
string will be the concatenation of the `root` property and the `base` property.
|
||
9 years ago
|
|
||
|
If both the `dir` and the `root` properties are not supplied, then the returned
|
||
|
string will be the contents of the `base` property.
|
||
|
|
||
|
If the `base` property is not supplied, a concatenation of the `name` property
|
||
|
and the `ext` property will be used as the `base` property.
|
||
9 years ago
|
|
||
9 years ago
|
Examples:
|
||
|
|
||
9 years ago
|
Some Posix system examples:
|
||
9 years ago
|
|
||
9 years ago
|
```js
|
||
9 years ago
|
// If `dir` and `base` are provided, `dir` + platform separator + `base`
|
||
|
// will be returned.
|
||
9 years ago
|
path.format({
|
||
9 years ago
|
dir: '/home/user/dir',
|
||
|
base: 'file.txt'
|
||
9 years ago
|
});
|
||
|
// returns '/home/user/dir/file.txt'
|
||
9 years ago
|
|
||
9 years ago
|
// `root` will be used if `dir` is not specified.
|
||
|
// `name` + `ext` will be used if `base` is not specified.
|
||
|
// If only `root` is provided or `dir` is equal to `root` then the
|
||
|
// platform separator will not be included.
|
||
9 years ago
|
path.format({
|
||
9 years ago
|
root: '/',
|
||
|
base: 'file.txt'
|
||
|
});
|
||
9 years ago
|
// returns '/file.txt'
|
||
10 years ago
|
|
||
9 years ago
|
path.format({
|
||
|
dir: '/',
|
||
|
root: '/',
|
||
|
name: 'file',
|
||
|
ext: '.txt'
|
||
|
});
|
||
|
// returns '/file.txt'
|
||
|
|
||
|
// `base` will be returned if `dir` or `root` are not provided.
|
||
|
path.format({
|
||
|
base: 'file.txt'
|
||
|
});
|
||
|
// returns 'file.txt'
|
||
|
```
|
||
9 years ago
|
An example on Windows:
|
||
|
|
||
|
```js
|
||
|
path.format({
|
||
|
root : "C:\\",
|
||
|
dir : "C:\\path\\dir",
|
||
|
base : "file.txt",
|
||
|
ext : ".txt",
|
||
|
name : "file"
|
||
|
})
|
||
|
// returns 'C:\\path\\dir\\file.txt'
|
||
|
```
|
||
|
|
||
12 years ago
|
## path.isAbsolute(path)
|
||
|
|
||
|
Determines whether `path` is an absolute path. An absolute path will always
|
||
9 years ago
|
resolve to the same location, regardless of the working directory. `path` must
|
||
|
be a string.
|
||
12 years ago
|
|
||
9 years ago
|
Examples on \*nix:
|
||
12 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.isAbsolute('/foo/bar') // true
|
||
|
path.isAbsolute('/baz/..') // true
|
||
|
path.isAbsolute('qux/') // false
|
||
|
path.isAbsolute('.') // false
|
||
|
```
|
||
12 years ago
|
|
||
9 years ago
|
Examples on Windows:
|
||
12 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.isAbsolute('//server') // true
|
||
|
path.isAbsolute('C:/foo/..') // true
|
||
|
path.isAbsolute('bar\\baz') // false
|
||
|
path.isAbsolute('.') // false
|
||
|
```
|
||
12 years ago
|
|
||
10 years ago
|
*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.
|
||
|
|
||
9 years ago
|
## path.join([path1][, path2][, ...])
|
||
14 years ago
|
|
||
9 years ago
|
Join all arguments together and normalize the resulting path.
|
||
14 years ago
|
|
||
9 years ago
|
All arguments must be strings. In v0.8, non-string arguments were
|
||
9 years ago
|
silently ignored. In v0.10 and up, an exception is thrown.
|
||
14 years ago
|
|
||
9 years ago
|
Examples:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
|
||
9 years ago
|
// returns '/foo/bar/baz/asdf'
|
||
14 years ago
|
|
||
9 years ago
|
path.join('foo', {}, 'bar')
|
||
|
// throws exception
|
||
|
TypeError: Arguments to path.join must be strings
|
||
|
```
|
||
14 years ago
|
|
||
9 years ago
|
*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.
|
||
10 years ago
|
|
||
9 years ago
|
## path.normalize(path)
|
||
14 years ago
|
|
||
9 years ago
|
Normalize a path, taking care of `'..'` and `'.'` parts. `path` must be a
|
||
|
string.
|
||
9 years ago
|
|
||
|
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.
|
||
14 years ago
|
|
||
|
Example:
|
||
|
|
||
9 years ago
|
```js
|
||
|
path.normalize('/foo/bar//baz/asdf/quux/..')
|
||
9 years ago
|
// returns '/foo/bar/baz/asdf'
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
*Note:* If the path string passed as argument is a zero-length string then `'.'`
|
||
|
will be returned, which represents the current working directory.
|
||
14 years ago
|
|
||
9 years ago
|
## path.parse(path)
|
||
14 years ago
|
|
||
9 years ago
|
Returns an object from a path. `path` must be a string.
|
||
14 years ago
|
|
||
9 years ago
|
An example on \*nix:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.parse('/home/user/dir/file.txt')
|
||
|
// returns
|
||
9 years ago
|
// {
|
||
|
// root : "/",
|
||
|
// dir : "/home/user/dir",
|
||
|
// base : "file.txt",
|
||
|
// ext : ".txt",
|
||
|
// name : "file"
|
||
|
// }
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
An example on Windows:
|
||
14 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.parse('C:\\path\\dir\\index.html')
|
||
|
// returns
|
||
9 years ago
|
// {
|
||
|
// root : "C:\\",
|
||
|
// dir : "C:\\path\\dir",
|
||
|
// base : "index.html",
|
||
|
// ext : ".html",
|
||
|
// name : "index"
|
||
|
// }
|
||
9 years ago
|
```
|
||
14 years ago
|
|
||
9 years ago
|
## path.posix
|
||
11 years ago
|
|
||
9 years ago
|
Provide access to aforementioned `path` methods but always interact in a posix
|
||
|
compatible way.
|
||
14 years ago
|
|
||
9 years ago
|
## path.relative(from, to)
|
||
13 years ago
|
|
||
9 years ago
|
Solve the relative path from `from` to `to`. `from` and `to` must be strings.
|
||
10 years ago
|
|
||
9 years ago
|
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:
|
||
13 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.resolve(from, path.relative(from, to)) == path.resolve(to)
|
||
|
```
|
||
13 years ago
|
|
||
9 years ago
|
Examples:
|
||
13 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
|
||
9 years ago
|
// returns '..\\..\\impl\\bbb'
|
||
13 years ago
|
|
||
9 years ago
|
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
|
||
9 years ago
|
// returns '../../impl/bbb'
|
||
9 years ago
|
```
|
||
12 years ago
|
|
||
9 years ago
|
*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.
|
||
12 years ago
|
|
||
9 years ago
|
## path.resolve([from ...], to)
|
||
12 years ago
|
|
||
9 years ago
|
Resolves `to` to an absolute path. All arguments must be strings.
|
||
12 years ago
|
|
||
9 years ago
|
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
|
||
9 years ago
|
gets resolved to the root directory. Empty string `from` arguments are
|
||
|
ignored.
|
||
12 years ago
|
|
||
9 years ago
|
Another way to think of it is as a sequence of `cd` commands in a shell.
|
||
12 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
|
||
|
```
|
||
12 years ago
|
|
||
9 years ago
|
Is similar to:
|
||
12 years ago
|
|
||
9 years ago
|
```
|
||
|
cd foo/bar
|
||
|
cd /tmp/file/
|
||
|
cd ..
|
||
|
cd a/../subfile
|
||
|
pwd
|
||
|
```
|
||
12 years ago
|
|
||
9 years ago
|
The difference is that the different paths don't need to exist and may also be
|
||
|
files.
|
||
11 years ago
|
|
||
9 years ago
|
Examples:
|
||
11 years ago
|
|
||
9 years ago
|
```js
|
||
|
path.resolve('/foo/bar', './baz')
|
||
9 years ago
|
// returns '/foo/bar/baz'
|
||
11 years ago
|
|
||
9 years ago
|
path.resolve('/foo/bar', '/tmp/file/')
|
||
9 years ago
|
// returns '/tmp/file'
|
||
11 years ago
|
|
||
9 years ago
|
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
|
||
|
// if currently in /home/myself/node, it returns
|
||
9 years ago
|
// '/home/myself/node/wwwroot/static_files/gif/image.gif'
|
||
9 years ago
|
```
|
||
11 years ago
|
|
||
9 years ago
|
## path.sep
|
||
11 years ago
|
|
||
9 years ago
|
The platform-specific file separator. `'\\'` or `'/'`.
|
||
11 years ago
|
|
||
9 years ago
|
An example on \*nix:
|
||
9 years ago
|
|
||
9 years ago
|
```js
|
||
|
'foo/bar/baz'.split(path.sep)
|
||
9 years ago
|
// returns ['foo', 'bar', 'baz']
|
||
9 years ago
|
```
|
||
11 years ago
|
|
||
9 years ago
|
An example on Windows:
|
||
12 years ago
|
|
||
9 years ago
|
```js
|
||
|
'foo\\bar\\baz'.split(path.sep)
|
||
9 years ago
|
// returns ['foo', 'bar', 'baz']
|
||
9 years ago
|
```
|
||
12 years ago
|
|
||
|
## path.win32
|
||
|
|
||
|
Provide access to aforementioned `path` methods but always interact in a win32
|
||
|
compatible way.
|
||
9 years ago
|
|
||
9 years ago
|
[`path.parse`]: #path_path_parse_path
|