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.
18 path.basename('/foo/bar/baz/asdf/quux.html')
22 path.basename('/foo/bar/baz/asdf/quux.html', '.html')
29 The platform-specific path delimiter, `;` or `':'`.
34 console.log(process.env.PATH)
35 // '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
37 process.env.PATH.split(path.delimiter)
39 ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
42 An example on Windows:
45 console.log(process.env.PATH)
46 // 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
48 process.env.PATH.split(path.delimiter)
50 ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
55 Return the directory name of a path. Similar to the Unix `dirname` command.
60 path.dirname('/foo/bar/baz/asdf/quux')
67 Return the extension of the path, from the last '.' to end of string
68 in the last portion of the path. If there is no '.' in the last portion
69 of the path or the first character of it is '.', then it returns
70 an empty string. Examples:
73 path.extname('index.html')
77 path.extname('index.coffee.md')
81 path.extname('index.')
89 path.extname('.index')
94 ## path.format(pathObject)
96 Returns a path string from an object, the opposite of [`path.parse`][].
101 dir : "/home/user/dir",
107 '/home/user/dir/file.txt'
109 ## path.isAbsolute(path)
111 Determines whether `path` is an absolute path. An absolute path will always
112 resolve to the same location, regardless of the working directory.
117 path.isAbsolute('/foo/bar') // true
118 path.isAbsolute('/baz/..') // true
119 path.isAbsolute('qux/') // false
120 path.isAbsolute('.') // false
126 path.isAbsolute('//server') // true
127 path.isAbsolute('C:/foo/..') // true
128 path.isAbsolute('bar\\baz') // false
129 path.isAbsolute('.') // false
132 *Note:* If the path string passed as parameter is a zero-length string, unlike
133 other path module functions, it will be used as-is and `false` will be
136 ## path.join([path1][, path2][, ...])
138 Join all arguments together and normalize the resulting path.
140 Arguments must be strings. In v0.8, non-string arguments were
141 silently ignored. In v0.10 and up, an exception is thrown.
146 path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
150 path.join('foo', {}, 'bar')
152 TypeError: Arguments to path.join must be strings
155 *Note:* If the arguments to `join` have zero-length strings, unlike other path
156 module functions, they will be ignored. If the joined path string is a
157 zero-length string then `'.'` will be returned, which represents the
158 current working directory.
162 Normalize a string path, taking care of `'..'` and `'.'` parts.
164 When multiple slashes are found, they're replaced by a single one;
165 when the path contains a trailing slash, it is preserved.
166 On Windows backslashes are used.
171 path.normalize('/foo/bar//baz/asdf/quux/..')
176 *Note:* If the path string passed as argument is a zero-length string then `'.'`
177 will be returned, which represents the current working directory.
179 ## path.parse(pathString)
181 Returns an object from a path string.
186 path.parse('/home/user/dir/file.txt')
190 dir : "/home/user/dir",
197 An example on Windows:
200 path.parse('C:\\path\\dir\\index.html')
204 dir : "C:\\path\\dir",
213 Provide access to aforementioned `path` methods but always interact in a posix
216 ## path.relative(from, to)
218 Solve the relative path from `from` to `to`.
220 At times we have two absolute paths, and we need to derive the relative
221 path from one to the other. This is actually the reverse transform of
222 `path.resolve`, which means we see that:
225 path.resolve(from, path.relative(from, to)) == path.resolve(to)
231 path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
235 path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
240 *Note:* If the arguments to `relative` have zero-length strings then the current
241 working directory will be used instead of the zero-length strings. If
242 both the paths are the same then a zero-length string will be returned.
244 ## path.resolve([from ...], to)
246 Resolves `to` to an absolute path.
248 If `to` isn't already absolute `from` arguments are prepended in right to left
249 order, until an absolute path is found. If after using all `from` paths still
250 no absolute path is found, the current working directory is used as well. The
251 resulting path is normalized, and trailing slashes are removed unless the path
252 gets resolved to the root directory. Non-string `from` arguments are ignored.
254 Another way to think of it is as a sequence of `cd` commands in a shell.
257 path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
270 The difference is that the different paths don't need to exist and may also be
276 path.resolve('/foo/bar', './baz')
280 path.resolve('/foo/bar', '/tmp/file/')
284 path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
285 // if currently in /home/myself/node, it returns
286 '/home/myself/node/wwwroot/static_files/gif/image.gif'
289 *Note:* If the arguments to `resolve` have zero-length strings then the current
290 working directory will be used instead of them.
294 The platform-specific file separator. `'\\'` or `'/'`.
299 'foo/bar/baz'.split(path.sep)
301 ['foo', 'bar', 'baz']
304 An example on Windows:
307 'foo\\bar\\baz'.split(path.sep)
309 ['foo', 'bar', 'baz']
314 Provide access to aforementioned `path` methods but always interact in a win32
317 [`path.parse`]: #path_path_parse_pathstring