Lines Matching full:path
1 # Path chapter
7 <!-- source_link=lib/path.js -->
9 The `node:path` module provides utilities for working with file and directory
13 const path = require('node:path');
18 The default operation of the `node:path` module varies based on the operating
20 a Windows operating system, the `node:path` module will assume that
23 So using `path.basename()` might yield different results on POSIX and Windows:
28 path.basename('C:\\temp\\myfile.html');
35 path.basename('C:\\temp\\myfile.html');
40 operating system, use [`path.win32`][]:
45 path.win32.basename('C:\\temp\\myfile.html');
50 operating system, use [`path.posix`][]:
55 path.posix.basename('/tmp/myfile.html');
60 This behavior can be observed when using a drive path without a backslash. For
61 example, `path.resolve('C:\\')` can potentially return a different result than
62 `path.resolve('C:')`. For more information, see
63 [this MSDN page][MSDN-Rel-Path].
65 ## `path.basename(path[, suffix])`
72 description: Passing a non-string as the `path` argument will throw now.
75 * `path` {string}
79 The `path.basename()` method returns the last portion of a `path`, similar to
80 the Unix `basename` command. Trailing [directory separators][`path.sep`] are
84 path.basename('/foo/bar/baz/asdf/quux.html');
87 path.basename('/foo/bar/baz/asdf/quux.html', '.html');
97 path.win32.basename('C:\\foo.html', '.html');
100 path.win32.basename('C:\\foo.HTML', '.html');
104 A [`TypeError`][] is thrown if `path` is not a string or if `suffix` is given
107 ## `path.delimiter`
115 Provides the platform-specific path delimiter:
123 console.log(process.env.PATH);
126 process.env.PATH.split(path.delimiter);
133 console.log(process.env.PATH);
136 process.env.PATH.split(path.delimiter);
140 ## `path.dirname(path)`
147 description: Passing a non-string as the `path` argument will throw now.
150 * `path` {string}
153 The `path.dirname()` method returns the directory name of a `path`, similar to
155 [`path.sep`][].
158 path.dirname('/foo/bar/baz/asdf/quux');
162 A [`TypeError`][] is thrown if `path` is not a string.
164 ## `path.extname(path)`
171 description: Passing a non-string as the `path` argument will throw now.
174 * `path` {string}
177 The `path.extname()` method returns the extension of the `path`, from the last
179 the `path`. If there is no `.` in the last portion of the `path`, or if
181 the basename of `path` (see `path.basename()`) , an empty string is returned.
184 path.extname('index.html');
187 path.extname('index.coffee.md');
190 path.extname('index.');
193 path.extname('index');
196 path.extname('.index');
199 path.extname('.index.md');
203 A [`TypeError`][] is thrown if `path` is not a string.
205 ## `path.format(pathObject)`
219 The `path.format()` method returns a path string from an object. This is the
220 opposite of [`path.parse()`][].
232 // `${dir}${path.sep}${base}`
234 path.format({
244 path.format({
252 path.format({
263 path.format({
264 dir: 'C:\\path\\dir',
267 // Returns: 'C:\\path\\dir\\file.txt'
270 ## `path.isAbsolute(path)`
276 * `path` {string}
279 The `path.isAbsolute()` method determines if `path` is an absolute path.
281 If the given `path` is a zero-length string, `false` will be returned.
286 path.isAbsolute('/foo/bar'); // true
287 path.isAbsolute('/baz/..'); // true
288 path.isAbsolute('qux/'); // false
289 path.isAbsolute('.'); // false
295 path.isAbsolute('//server'); // true
296 path.isAbsolute('\\\\server'); // true
297 path.isAbsolute('C:/foo/..'); // true
298 path.isAbsolute('C:\\foo\\..'); // true
299 path.isAbsolute('bar\\baz'); // false
300 path.isAbsolute('bar/baz'); // false
301 path.isAbsolute('.'); // false
304 A [`TypeError`][] is thrown if `path` is not a string.
306 ## `path.join([...paths])`
312 * `...paths` {string} A sequence of path segments
315 The `path.join()` method joins all given `path` segments together using the
316 platform-specific separator as a delimiter, then normalizes the resulting path.
318 Zero-length `path` segments are ignored. If the joined path string is a
323 path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
326 path.join('foo', {}, 'bar');
327 // Throws 'TypeError: Path must be a string. Received {}'
330 A [`TypeError`][] is thrown if any of the path segments is not a string.
332 ## `path.normalize(path)`
338 * `path` {string}
341 The `path.normalize()` method normalizes the given `path`, resolving `'..'` and
344 When multiple, sequential path segment separation characters are found (e.g.
346 instance of the platform-specific path segment separator (`/` on POSIX and
349 If the `path` is a zero-length string, `'.'` is returned, representing the
355 path.normalize('/foo/bar//baz/asdf/quux/..');
362 path.normalize('C:\\temp\\\\foo\\bar\\..\\');
366 Since Windows recognizes multiple path separators, both separators will be
370 path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
374 A [`TypeError`][] is thrown if `path` is not a string.
376 ## `path.parse(path)`
382 * `path` {string}
385 The `path.parse()` method returns an object whose properties represent
386 significant elements of the `path`. Trailing directory separators are ignored,
387 see [`path.sep`][].
400 path.parse('/home/user/dir/file.txt');
422 path.parse('C:\\path\\dir\\file.txt');
425 // dir: 'C:\\path\\dir',
436 " C:\ path\dir \ file .txt "
441 A [`TypeError`][] is thrown if `path` is not a string.
443 ## `path.posix`
450 description: Exposed as `require('path/posix')`.
455 The `path.posix` property provides access to POSIX specific implementations
456 of the `path` methods.
458 The API is accessible via `require('node:path').posix` or `require('node:path/posix')`.
460 ## `path.relative(from, to)`
475 The `path.relative()` method returns the relative path from `from` to `to` based
477 path (after calling `path.resolve()` on each), a zero-length string is returned.
485 path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
492 path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
498 ## `path.resolve([...paths])`
504 * `...paths` {string} A sequence of paths or path segments
507 The `path.resolve()` method resolves a sequence of paths or path segments into
508 an absolute path.
511 subsequent `path` prepended until an absolute path is constructed.
512 For instance, given the sequence of path segments: `/foo`, `/bar`, `baz`,
513 calling `path.resolve('/foo', '/bar', 'baz')` would return `/bar/baz`
514 because `'baz'` is not an absolute path but `'/bar' + '/' + 'baz'` is.
516 If, after processing all given `path` segments, an absolute path has not yet
519 The resulting path is normalized and trailing slashes are removed unless the
520 path is resolved to the root directory.
522 Zero-length `path` segments are ignored.
524 If no `path` segments are passed, `path.resolve()` will return the absolute path
528 path.resolve('/foo/bar', './baz');
531 path.resolve('/foo/bar', '/tmp/file/');
534 path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
541 ## `path.sep`
549 Provides the platform-specific path segment separator:
557 'foo/bar/baz'.split(path.sep);
564 'foo\\bar\\baz'.split(path.sep);
569 as path segment separators; however, the `path` methods only add backward
572 ## `path.toNamespacedPath(path)`
578 * `path` {string}
581 On Windows systems only, returns an equivalent [namespace-prefixed path][] for
582 the given `path`. If `path` is not a string, `path` will be returned without
586 method is non-operational and always returns `path` without modifications.
588 ## `path.win32`
595 description: Exposed as `require('path/win32')`.
600 The `path.win32` property provides access to Windows-specific implementations
601 of the `path` methods.
603 The API is accessible via `require('node:path').win32` or `require('node:path/win32')`.
605 [MSDN-Rel-Path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualif…
607 [`path.parse()`]: #pathparsepath
608 [`path.posix`]: #pathposix
609 [`path.sep`]: #pathsep
610 [`path.win32`]: #pathwin32
611 [namespace-prefixed path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#na…