{ "source": "doc/api/path.markdown", "modules": [ { "textRaw": "Path", "name": "path", "stability": 2, "stabilityText": "Stable", "desc": "

This module contains utilities for handling and transforming file\npaths. Almost all these methods perform only string transformations.\nThe file system is not consulted to check whether paths are valid.\n\n

\n

Use require('path') to use this module. The following methods are provided:\n\n

\n", "methods": [ { "textRaw": "path.basename(p[, ext])", "type": "method", "name": "basename", "desc": "

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

\n

Example:\n\n

\n
path.basename('/foo/bar/baz/asdf/quux.html')\n// returns\n'quux.html'\n\npath.basename('/foo/bar/baz/asdf/quux.html', '.html')\n// returns\n'quux'
\n", "signatures": [ { "params": [ { "name": "p" }, { "name": "ext", "optional": true } ] } ] }, { "textRaw": "path.dirname(p)", "type": "method", "name": "dirname", "desc": "

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

\n

Example:\n\n

\n
path.dirname('/foo/bar/baz/asdf/quux')\n// returns\n'/foo/bar/baz/asdf'
\n", "signatures": [ { "params": [ { "name": "p" } ] } ] }, { "textRaw": "path.extname(p)", "type": "method", "name": "extname", "desc": "

Return the extension of the path, from the last '.' to end of string\nin the last portion of the path. If there is no '.' in the last portion\nof the path or the first character of it is '.', then it returns\nan empty string. Examples:\n\n

\n
path.extname('index.html')\n// returns\n'.html'\n\npath.extname('index.coffee.md')\n// returns\n'.md'\n\npath.extname('index.')\n// returns\n'.'\n\npath.extname('index')\n// returns\n''\n\npath.extname('.index')\n// returns\n''
\n", "signatures": [ { "params": [ { "name": "p" } ] } ] }, { "textRaw": "path.format(pathObject)", "type": "method", "name": "format", "desc": "

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

\n
path.format({\n    root : "/",\n    dir : "/home/user/dir",\n    base : "file.txt",\n    ext : ".txt",\n    name : "file"\n})\n// returns\n'/home/user/dir/file.txt'
\n", "signatures": [ { "params": [ { "name": "pathObject" } ] } ] }, { "textRaw": "path.isAbsolute(path)", "type": "method", "name": "isAbsolute", "desc": "

Determines whether path is an absolute path. An absolute path will always\nresolve to the same location, regardless of the working directory.\n\n

\n

Posix examples:\n\n

\n
path.isAbsolute('/foo/bar') // true\npath.isAbsolute('/baz/..')  // true\npath.isAbsolute('qux/')     // false\npath.isAbsolute('.')        // false
\n

Windows examples:\n\n

\n
path.isAbsolute('//server')  // true\npath.isAbsolute('C:/foo/..') // true\npath.isAbsolute('bar\\\\baz')  // false\npath.isAbsolute('.')         // false
\n

Note: If the path string passed as parameter is a zero-length string, unlike\n other path module functions, it will be used as-is and false will be\n returned.\n\n

\n", "signatures": [ { "params": [ { "name": "path" } ] } ] }, { "textRaw": "path.join([path1][, path2][, ...])", "type": "method", "name": "join", "desc": "

Join all arguments together and normalize the resulting path.\n\n

\n

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

\n

Example:\n\n

\n
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')\n// returns\n'/foo/bar/baz/asdf'\n\npath.join('foo', {}, 'bar')\n// throws exception\nTypeError: Arguments to path.join must be strings
\n

Note: If the arguments to join have zero-length strings, unlike other path\n module functions, they will be ignored. If the joined path string is a\n zero-length string then '.' will be returned, which represents the\n current working directory.\n\n

\n", "signatures": [ { "params": [ { "name": "path1", "optional": true }, { "name": "path2", "optional": true }, { "name": "...", "optional": true } ] } ] }, { "textRaw": "path.normalize(p)", "type": "method", "name": "normalize", "desc": "

Normalize a string path, taking care of '..' and '.' parts.\n\n

\n

When multiple slashes are found, they're replaced by a single one;\nwhen the path contains a trailing slash, it is preserved.\nOn Windows backslashes are used.\n\n

\n

Example:\n\n

\n
path.normalize('/foo/bar//baz/asdf/quux/..')\n// returns\n'/foo/bar/baz/asdf'
\n

Note: If the path string passed as argument is a zero-length string then '.'\n will be returned, which represents the current working directory.\n\n

\n", "signatures": [ { "params": [ { "name": "p" } ] } ] }, { "textRaw": "path.parse(pathString)", "type": "method", "name": "parse", "desc": "

Returns an object from a path string.\n\n

\n

An example on *nix:\n\n

\n
path.parse('/home/user/dir/file.txt')\n// returns\n{\n    root : "/",\n    dir : "/home/user/dir",\n    base : "file.txt",\n    ext : ".txt",\n    name : "file"\n}
\n

An example on Windows:\n\n

\n
path.parse('C:\\\\path\\\\dir\\\\index.html')\n// returns\n{\n    root : "C:\\\\",\n    dir : "C:\\\\path\\\\dir",\n    base : "index.html",\n    ext : ".html",\n    name : "index"\n}
\n", "signatures": [ { "params": [ { "name": "pathString" } ] } ] }, { "textRaw": "path.relative(from, to)", "type": "method", "name": "relative", "desc": "

Solve the relative path from from to to.\n\n

\n

At times we have two absolute paths, and we need to derive the relative\npath from one to the other. This is actually the reverse transform of\npath.resolve, which means we see that:\n\n

\n
path.resolve(from, path.relative(from, to)) == path.resolve(to)
\n

Examples:\n\n

\n
path.relative('C:\\\\orandea\\\\test\\\\aaa', 'C:\\\\orandea\\\\impl\\\\bbb')\n// returns\n'..\\\\..\\\\impl\\\\bbb'\n\npath.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')\n// returns\n'../../impl/bbb'
\n

Note: If the arguments to relative have zero-length strings then the current\n working directory will be used instead of the zero-length strings. If\n both the paths are the same then a zero-length string will be returned.\n\n

\n", "signatures": [ { "params": [ { "name": "from" }, { "name": "to" } ] } ] }, { "textRaw": "path.resolve([from ...], to)", "type": "method", "name": "resolve", "desc": "

Resolves to to an absolute path.\n\n

\n

If to isn't already absolute from arguments are prepended in right to left\norder, until an absolute path is found. If after using all from paths still\nno absolute path is found, the current working directory is used as well. The\nresulting path is normalized, and trailing slashes are removed unless the path\ngets resolved to the root directory. Non-string from arguments are ignored.\n\n

\n

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

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

Is similar to:\n\n

\n
cd foo/bar\ncd /tmp/file/\ncd ..\ncd a/../subfile\npwd
\n

The difference is that the different paths don't need to exist and may also be\nfiles.\n\n

\n

Examples:\n\n

\n
path.resolve('/foo/bar', './baz')\n// returns\n'/foo/bar/baz'\n\npath.resolve('/foo/bar', '/tmp/file/')\n// returns\n'/tmp/file'\n\npath.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')\n// if currently in /home/myself/node, it returns\n'/home/myself/node/wwwroot/static_files/gif/image.gif'
\n

Note: If the arguments to resolve have zero-length strings then the current\n working directory will be used instead of them.\n\n

\n", "signatures": [ { "params": [ { "name": "from ...", "optional": true }, { "name": "to" } ] } ] } ], "properties": [ { "textRaw": "path.delimiter", "name": "delimiter", "desc": "

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

\n

An example on *nix:\n\n

\n
console.log(process.env.PATH)\n// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'\n\nprocess.env.PATH.split(path.delimiter)\n// returns\n['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
\n

An example on Windows:\n\n

\n
console.log(process.env.PATH)\n// 'C:\\Windows\\system32;C:\\Windows;C:\\Program Files\\node\\'\n\nprocess.env.PATH.split(path.delimiter)\n// returns\n['C:\\\\Windows\\\\system32', 'C:\\\\Windows', 'C:\\\\Program Files\\\\node\\\\']
\n" }, { "textRaw": "path.posix", "name": "posix", "desc": "

Provide access to aforementioned path methods but always interact in a posix\ncompatible way.\n\n

\n" }, { "textRaw": "path.sep", "name": "sep", "desc": "

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

\n

An example on *nix:\n\n

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

An example on Windows:\n\n

\n
'foo\\\\bar\\\\baz'.split(path.sep)\n// returns\n['foo', 'bar', 'baz']
\n" }, { "textRaw": "path.win32", "name": "win32", "desc": "

Provide access to aforementioned path methods but always interact in a win32\ncompatible way.\n

\n" } ], "type": "module", "displayName": "Path" } ] }