5 This module contains utilities for handling and transforming file
6 paths. Almost all these methods perform only string transformations.
7 The file system is not consulted to check whether paths are valid.
9 Use `require('path')` to use this module. The following methods are provided:
11 ## path.basename(p[, ext])
13 Return the last portion of a path. Similar to the Unix `basename` command.
17 path.basename('/foo/bar/baz/asdf/quux.html')
21 path.basename('/foo/bar/baz/asdf/quux.html', '.html')
27 The platform-specific path delimiter, `;` or `':'`.
31 console.log(process.env.PATH)
32 // '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
34 process.env.PATH.split(path.delimiter)
36 ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
38 An example on Windows:
40 console.log(process.env.PATH)
41 // 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
43 process.env.PATH.split(path.delimiter)
45 ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
49 Return the directory name of a path. Similar to the Unix `dirname` command.
53 path.dirname('/foo/bar/baz/asdf/quux')
59 Return the extension of the path, from the last '.' to end of string
60 in the last portion of the path. If there is no '.' in the last portion
61 of the path or the first character of it is '.', then it returns
62 an empty string. Examples:
64 path.extname('index.html')
68 path.extname('index.coffee.md')
72 path.extname('index.')
80 path.extname('.index')
84 ## path.format(pathObject)
86 Returns a path string from an object, the opposite of [`path.parse`][].
90 dir : "/home/user/dir",
96 '/home/user/dir/file.txt'
98 ## path.isAbsolute(path)
100 Determines whether `path` is an absolute path. An absolute path will always
101 resolve to the same location, regardless of the working directory.
105 path.isAbsolute('/foo/bar') // true
106 path.isAbsolute('/baz/..') // true
107 path.isAbsolute('qux/') // false
108 path.isAbsolute('.') // false
112 path.isAbsolute('//server') // true
113 path.isAbsolute('C:/foo/..') // true
114 path.isAbsolute('bar\\baz') // false
115 path.isAbsolute('.') // false
117 *Note:* If the path string passed as parameter is a zero-length string, unlike
118 other path module functions, it will be used as-is and `false` will be
121 ## path.join([path1][, path2][, ...])
123 Join all arguments together and normalize the resulting path.
125 Arguments must be strings. In v0.8, non-string arguments were
126 silently ignored. In v0.10 and up, an exception is thrown.
130 path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
134 path.join('foo', {}, 'bar')
136 TypeError: Arguments to path.join must be strings
138 *Note:* If the arguments to `join` have zero-length strings, unlike other path
139 module functions, they will be ignored. If the joined path string is a
140 zero-length string then `'.'` will be returned, which represents the
141 current working directory.
145 Normalize a string path, taking care of `'..'` and `'.'` parts.
147 When multiple slashes are found, they're replaced by a single one;
148 when the path contains a trailing slash, it is preserved.
149 On Windows backslashes are used.
153 path.normalize('/foo/bar//baz/asdf/quux/..')
157 *Note:* If the path string passed as argument is a zero-length string then `'.'`
158 will be returned, which represents the current working directory.
160 ## path.parse(pathString)
162 Returns an object from a path string.
166 path.parse('/home/user/dir/file.txt')
170 dir : "/home/user/dir",
176 An example on Windows:
178 path.parse('C:\\path\\dir\\index.html')
182 dir : "C:\\path\\dir",
190 Provide access to aforementioned `path` methods but always interact in a posix
193 ## path.relative(from, to)
195 Solve the relative path from `from` to `to`.
197 At times we have two absolute paths, and we need to derive the relative
198 path from one to the other. This is actually the reverse transform of
199 `path.resolve`, which means we see that:
201 path.resolve(from, path.relative(from, to)) == path.resolve(to)
205 path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
209 path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
213 *Note:* If the arguments to `relative` have zero-length strings then the current
214 working directory will be used instead of the zero-length strings. If
215 both the paths are the same then a zero-length string will be returned.
217 ## path.resolve([from ...], to)
219 Resolves `to` to an absolute path.
221 If `to` isn't already absolute `from` arguments are prepended in right to left
222 order, until an absolute path is found. If after using all `from` paths still
223 no absolute path is found, the current working directory is used as well. The
224 resulting path is normalized, and trailing slashes are removed unless the path
225 gets resolved to the root directory. Non-string `from` arguments are ignored.
227 Another way to think of it is as a sequence of `cd` commands in a shell.
229 path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
239 The difference is that the different paths don't need to exist and may also be
244 path.resolve('/foo/bar', './baz')
248 path.resolve('/foo/bar', '/tmp/file/')
252 path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
253 // if currently in /home/myself/node, it returns
254 '/home/myself/node/wwwroot/static_files/gif/image.gif'
256 *Note:* If the arguments to `resolve` have zero-length strings then the current
257 working directory will be used instead of them.
261 The platform-specific file separator. `'\\'` or `'/'`.
265 'foo/bar/baz'.split(path.sep)
267 ['foo', 'bar', 'baz']
269 An example on Windows:
271 'foo\\bar\\baz'.split(path.sep)
273 ['foo', 'bar', 'baz']
277 Provide access to aforementioned `path` methods but always interact in a win32
280 [`path.parse`]: #path_path_parse_pathstring