2010-10-28 14:18:16 +02:00
|
|
|
## Path
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
`path.exists` and `path.existsSync` are the exceptions, and should
|
|
|
|
logically be found in the fs module as they do access the file system.
|
|
|
|
|
|
|
|
Use `require('path')` to use this module. The following methods are provided:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-01-06 06:39:00 +01:00
|
|
|
### path.normalize(p)
|
|
|
|
|
|
|
|
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.
|
|
|
|
On windows backslashes are used.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
path.normalize('/foo/bar//baz/asdf/quux/..')
|
|
|
|
// returns
|
|
|
|
'/foo/bar/baz/asdf'
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### path.join([path1], [path2], [...])
|
|
|
|
|
2011-01-06 06:39:00 +01:00
|
|
|
Join all arguments together and normalize the resulting path.
|
2011-07-04 18:21:38 +02:00
|
|
|
Non-string arguments are ignored.
|
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')
|
|
|
|
// returns
|
|
|
|
'foo/bar'
|
|
|
|
|
2011-01-06 06:39:00 +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
|
|
|
|
resulting path is normalized, and trailing slashes are removed unless the path
|
2011-07-04 18:21:38 +02:00
|
|
|
gets resolved to the root directory. Non-string 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
|
|
|
|
2011-08-04 05:39:19 +02:00
|
|
|
### path.relative(from, to)
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### path.dirname(p)
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
### path.basename(p, [ext])
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
### path.extname(p)
|
|
|
|
|
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'
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
path.extname('index.')
|
|
|
|
// returns
|
|
|
|
'.'
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
path.extname('index')
|
|
|
|
// returns
|
|
|
|
''
|
|
|
|
|
|
|
|
### path.exists(p, [callback])
|
|
|
|
|
2011-08-31 15:12:34 +02:00
|
|
|
Test whether or not the given path exists by checking with the file system.
|
|
|
|
Then call the `callback` argument with either true or false. Example:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
path.exists('/etc/passwd', function (exists) {
|
|
|
|
util.debug(exists ? "it's there" : "no passwd!");
|
|
|
|
});
|
2011-02-20 22:11:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
### path.existsSync(p)
|
|
|
|
|
|
|
|
Synchronous version of `path.exists`.
|