2012-02-27 20:09:34 +01:00
|
|
|
# Path
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-03 00:14:03 +01:00
|
|
|
Stability: 3 - Stable
|
|
|
|
|
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
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.normalize(p)
|
2011-01-06 06:39:00 +01:00
|
|
|
|
|
|
|
Normalize a string path, taking care of `'..'` and `'.'` parts.
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
When multiple slashes are found, they're replaced by a single one;
|
2011-01-06 06:39:00 +01:00
|
|
|
when the path contains a trailing slash, it is preserved.
|
2012-10-01 22:10:32 +02:00
|
|
|
On Windows backslashes are used.
|
2011-01-06 06:39:00 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
path.normalize('/foo/bar//baz/asdf/quux/..')
|
|
|
|
// returns
|
|
|
|
'/foo/bar/baz/asdf'
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.join([path1], [path2], [...])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-06 06:39:00 +01:00
|
|
|
Join all arguments together and normalize the resulting path.
|
2013-03-13 18:27:09 +01:00
|
|
|
|
|
|
|
Arguments must be strings. In v0.8, non-string arguments were
|
|
|
|
silently ignored. In v0.10 and up, an exception is thrown.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2011-07-04 18:21:38 +02:00
|
|
|
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
|
|
|
|
// returns
|
2010-10-28 14:18:16 +02:00
|
|
|
'/foo/bar/baz/asdf'
|
|
|
|
|
2011-07-04 18:21:38 +02:00
|
|
|
path.join('foo', {}, 'bar')
|
2013-04-09 22:36:34 +02:00
|
|
|
// throws exception
|
|
|
|
TypeError: Arguments to path.join must be strings
|
2011-07-04 18:21:38 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.resolve([from ...], to)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-11 02:57:25 +01:00
|
|
|
Resolves `to` to an absolute path.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-11 02:57:25 +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
|
2012-10-01 22:10:32 +02:00
|
|
|
resulting path is normalized, and trailing slashes are removed unless the path
|
2014-06-25 19:08:46 +02:00
|
|
|
gets resolved to the root directory. Non-string `from` arguments are ignored.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-11 02:57:25 +01:00
|
|
|
Another way to think of it is as a sequence of `cd` commands in a shell.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-11 02:57:25 +01:00
|
|
|
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-11 02:57:25 +01:00
|
|
|
Is similar to:
|
|
|
|
|
|
|
|
cd foo/bar
|
|
|
|
cd /tmp/file/
|
|
|
|
cd ..
|
|
|
|
cd a/../subfile
|
|
|
|
pwd
|
|
|
|
|
|
|
|
The difference is that the different paths don't need to exist and may also be
|
|
|
|
files.
|
|
|
|
|
|
|
|
Examples:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-06 06:39:00 +01:00
|
|
|
path.resolve('/foo/bar', './baz')
|
|
|
|
// returns
|
2011-01-11 02:57:25 +01:00
|
|
|
'/foo/bar/baz'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-06 06:39:00 +01:00
|
|
|
path.resolve('/foo/bar', '/tmp/file/')
|
2010-10-28 14:18:16 +02:00
|
|
|
// returns
|
2011-01-06 06:39:00 +01:00
|
|
|
'/tmp/file'
|
|
|
|
|
|
|
|
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
|
2011-01-11 02:57:25 +01:00
|
|
|
// if currently in /home/myself/node, it returns
|
|
|
|
'/home/myself/node/wwwroot/static_files/gif/image.gif'
|
2010-10-28 14:18:16 +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:
|
|
|
|
|
|
|
|
path.isAbsolute('/foo/bar') // true
|
|
|
|
path.isAbsolute('/baz/..') // true
|
|
|
|
path.isAbsolute('qux/') // false
|
|
|
|
path.isAbsolute('.') // false
|
|
|
|
|
|
|
|
Windows examples:
|
|
|
|
|
|
|
|
path.isAbsolute('//server') // true
|
|
|
|
path.isAbsolute('C:/foo/..') // true
|
|
|
|
path.isAbsolute('bar\\baz') // false
|
|
|
|
path.isAbsolute('.') // false
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.relative(from, to)
|
2011-08-04 05:39:19 +02:00
|
|
|
|
|
|
|
Solve the relative path from `from` to `to`.
|
|
|
|
|
2011-08-31 15:12:34 +02: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:
|
2011-08-04 05:39:19 +02:00
|
|
|
|
|
|
|
path.resolve(from, path.relative(from, to)) == path.resolve(to)
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
|
|
|
|
// returns
|
|
|
|
'..\\..\\impl\\bbb'
|
|
|
|
|
|
|
|
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
|
|
|
|
// returns
|
|
|
|
'../../impl/bbb'
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.dirname(p)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Return the directory name of a path. Similar to the Unix `dirname` command.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
path.dirname('/foo/bar/baz/asdf/quux')
|
|
|
|
// returns
|
|
|
|
'/foo/bar/baz/asdf'
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.basename(p, [ext])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Return the last portion of a path. Similar to the Unix `basename` command.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html')
|
|
|
|
// returns
|
|
|
|
'quux.html'
|
|
|
|
|
|
|
|
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
|
|
|
|
// returns
|
|
|
|
'quux'
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## path.extname(p)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-08-31 15:12:34 +02: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:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
path.extname('index.html')
|
2010-11-21 23:22:34 +01:00
|
|
|
// returns
|
2010-10-28 14:18:16 +02:00
|
|
|
'.html'
|
|
|
|
|
2014-02-01 16:10:25 +01:00
|
|
|
path.extname('index.coffee.md')
|
|
|
|
// returns
|
|
|
|
'.md'
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
path.extname('index.')
|
|
|
|
// returns
|
|
|
|
'.'
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
path.extname('index')
|
|
|
|
// returns
|
|
|
|
''
|
2012-04-02 18:31:21 +02:00
|
|
|
|
|
|
|
## path.sep
|
|
|
|
|
|
|
|
The platform-specific file separator. `'\\'` or `'/'`.
|
|
|
|
|
2012-10-01 22:10:32 +02:00
|
|
|
An example on *nix:
|
2012-04-02 18:31:21 +02:00
|
|
|
|
|
|
|
'foo/bar/baz'.split(path.sep)
|
|
|
|
// returns
|
|
|
|
['foo', 'bar', 'baz']
|
|
|
|
|
2012-10-01 22:10:32 +02:00
|
|
|
An example on Windows:
|
2012-04-02 18:31:21 +02:00
|
|
|
|
|
|
|
'foo\\bar\\baz'.split(path.sep)
|
|
|
|
// returns
|
|
|
|
['foo', 'bar', 'baz']
|
2012-10-01 22:10:32 +02:00
|
|
|
|
|
|
|
## path.delimiter
|
|
|
|
|
|
|
|
The platform-specific path delimiter, `;` or `':'`.
|
|
|
|
|
|
|
|
An example on *nix:
|
|
|
|
|
|
|
|
console.log(process.env.PATH)
|
|
|
|
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
|
|
|
|
|
|
|
|
process.env.PATH.split(path.delimiter)
|
|
|
|
// returns
|
|
|
|
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
|
|
|
|
|
|
|
|
An example on Windows:
|
|
|
|
|
|
|
|
console.log(process.env.PATH)
|
|
|
|
// 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\'
|
|
|
|
|
|
|
|
process.env.PATH.split(path.delimiter)
|
|
|
|
// returns
|
|
|
|
['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']
|