{ "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
\nUse require('path')
to use this module. The following methods are provided:\n\n
Normalize a string path, taking care of '..'
and '.'
parts.\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
\nExample:\n\n
\npath.normalize('/foo/bar//baz/asdf/quux/..')\n// returns\n'/foo/bar/baz/asdf'
\nNote: 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
Join all arguments together and normalize the resulting path.\n\n
\nArguments must be strings. In v0.8, non-string arguments were\nsilently ignored. In v0.10 and up, an exception is thrown.\n\n
\nExample:\n\n
\npath.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
\nNote: 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
Resolves to
to an absolute path.\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
Another way to think of it is as a sequence of cd
commands in a shell.\n\n
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
\nIs similar to:\n\n
\ncd foo/bar\ncd /tmp/file/\ncd ..\ncd a/../subfile\npwd
\nThe difference is that the different paths don't need to exist and may also be\nfiles.\n\n
\nExamples:\n\n
\npath.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'
\nNote: If the arguments to resolve
have zero-length strings then the current\n working directory will be used instead of them.\n\n
Determines whether path
is an absolute path. An absolute path will always\nresolve to the same location, regardless of the working directory.\n\n
Posix examples:\n\n
\npath.isAbsolute('/foo/bar') // true\npath.isAbsolute('/baz/..') // true\npath.isAbsolute('qux/') // false\npath.isAbsolute('.') // false
\nWindows examples:\n\n
\npath.isAbsolute('//server') // true\npath.isAbsolute('C:/foo/..') // true\npath.isAbsolute('bar\\\\baz') // false\npath.isAbsolute('.') // false
\nNote: 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
Solve the relative path from from
to to
.\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
path.resolve(from, path.relative(from, to)) == path.resolve(to)
\nExamples:\n\n
\npath.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'
\nNote: 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
Return the directory name of a path. Similar to the Unix dirname
command.\n\n
Example:\n\n
\npath.dirname('/foo/bar/baz/asdf/quux')\n// returns\n'/foo/bar/baz/asdf'
\n",
"signatures": [
{
"params": [
{
"name": "p"
}
]
}
]
},
{
"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
Example:\n\n
\npath.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.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
\npath.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.parse(pathString)",
"type": "method",
"name": "parse",
"desc": "Returns an object from a path string.\n\n
\nAn example on *nix:\n\n
\npath.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}
\nAn example on Windows:\n\n
\npath.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.format(pathObject)",
"type": "method",
"name": "format",
"desc": "Returns a path string from an object, the opposite of path.parse
above.\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"
}
]
}
]
}
],
"properties": [
{
"textRaw": "path.sep",
"name": "sep",
"desc": "The platform-specific file separator. '\\\\'
or '/'
.\n\n
An example on *nix:\n\n
\n'foo/bar/baz'.split(path.sep)\n// returns\n['foo', 'bar', 'baz']
\nAn example on Windows:\n\n
\n'foo\\\\bar\\\\baz'.split(path.sep)\n// returns\n['foo', 'bar', 'baz']
\n"
},
{
"textRaw": "path.delimiter",
"name": "delimiter",
"desc": "The platform-specific path delimiter, ;
or ':'
.\n\n
An example on *nix:\n\n
\nconsole.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']
\nAn example on Windows:\n\n
\nconsole.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
Provide access to aforementioned path
methods but always interact in a win32\ncompatible way.\n