Path

  1. Stability: 3 - Stable

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:

path.normalize(p)

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.

Example:

  1. path.normalize('/foo/bar//baz/asdf/quux/..')
  2. // returns
  3. '/foo/bar/baz/asdf'

path.join([path1][, path2][, …])

Join all arguments together and normalize the resulting path.

Arguments must be strings. In v0.8, non-string arguments were
silently ignored. In v0.10 and up, an exception is thrown.

Example:

  1. path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
  2. // returns
  3. '/foo/bar/baz/asdf'
  4. path.join('foo', {}, 'bar')
  5. // throws exception
  6. TypeError: Arguments to path.join must be strings

path.resolve([from …], to)

Resolves to to an absolute path.

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.

Another way to think of it is as a sequence of cd commands in a shell.

  1. path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')

Is similar to:

  1. cd foo/bar
  2. cd /tmp/file/
  3. cd ..
  4. cd a/../subfile
  5. pwd

The difference is that the different paths don’t need to exist and may also be
files.

Examples:

  1. path.resolve('/foo/bar', './baz')
  2. // returns
  3. '/foo/bar/baz'
  4. path.resolve('/foo/bar', '/tmp/file/')
  5. // returns
  6. '/tmp/file'
  7. path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
  8. // if currently in /home/myself/node, it returns
  9. '/home/myself/node/wwwroot/static_files/gif/image.gif'

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:

  1. path.isAbsolute('/foo/bar') // true
  2. path.isAbsolute('/baz/..') // true
  3. path.isAbsolute('qux/') // false
  4. path.isAbsolute('.') // false

Windows examples:

  1. path.isAbsolute('//server') // true
  2. path.isAbsolute('C:/foo/..') // true
  3. path.isAbsolute('bar\\baz') // false
  4. path.isAbsolute('.') // false

path.relative(from, to)

Solve the relative path from from to to.

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:

  1. path.resolve(from, path.relative(from, to)) == path.resolve(to)

Examples:

  1. path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
  2. // returns
  3. '..\\..\\impl\\bbb'
  4. path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
  5. // returns
  6. '../../impl/bbb'

path.dirname(p)

Return the directory name of a path. Similar to the Unix dirname command.

Example:

  1. path.dirname('/foo/bar/baz/asdf/quux')
  2. // returns
  3. '/foo/bar/baz/asdf'

path.basename(p[, ext])

Return the last portion of a path. Similar to the Unix basename command.

Example:

  1. path.basename('/foo/bar/baz/asdf/quux.html')
  2. // returns
  3. 'quux.html'
  4. path.basename('/foo/bar/baz/asdf/quux.html', '.html')
  5. // returns
  6. 'quux'

path.extname(p)

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:

  1. path.extname('index.html')
  2. // returns
  3. '.html'
  4. path.extname('index.coffee.md')
  5. // returns
  6. '.md'
  7. path.extname('index.')
  8. // returns
  9. '.'
  10. path.extname('index')
  11. // returns
  12. ''

path.sep

The platform-specific file separator. '\\' or '/'.

An example on *nix:

  1. 'foo/bar/baz'.split(path.sep)
  2. // returns
  3. ['foo', 'bar', 'baz']

An example on Windows:

  1. 'foo\\bar\\baz'.split(path.sep)
  2. // returns
  3. ['foo', 'bar', 'baz']

path.delimiter

The platform-specific path delimiter, ; or ':'.

An example on *nix:

  1. console.log(process.env.PATH)
  2. // '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
  3. process.env.PATH.split(path.delimiter)
  4. // returns
  5. ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']

An example on Windows:

  1. console.log(process.env.PATH)
  2. // 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\'
  3. process.env.PATH.split(path.delimiter)
  4. // returns
  5. ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\nodejs\\']

path.parse(pathString)

Returns an object from a path string.

An example on *nix:

  1. path.parse('/home/user/dir/file.txt')
  2. // returns
  3. {
  4. root : "/",
  5. dir : "/home/user/dir",
  6. base : "file.txt",
  7. ext : ".txt",
  8. name : "file"
  9. }

An example on Windows:

  1. path.parse('C:\\path\\dir\\index.html')
  2. // returns
  3. {
  4. root : "C:\\",
  5. dir : "C:\\path\\dir",
  6. base : "index.html",
  7. ext : ".html",
  8. name : "index"
  9. }

path.format(pathObject)

Returns a path string from an object, the opposite of path.parse above.

  1. path.format({
  2. root : "/",
  3. dir : "/home/user/dir",
  4. base : "file.txt",
  5. ext : ".txt",
  6. name : "file"
  7. })
  8. // returns
  9. '/home/user/dir/file.txt'

path.posix

Provide access to aforementioned path methods but always interact in a posix
compatible way.

path.win32

Provide access to aforementioned path methods but always interact in a win32
compatible way.