API reference

path

10/10Coverage7/10Strict (zero caveats)

Node's `path` module for portable filesystem path manipulation, import-gated as a virtual built-in (`import path from 'path'`). Both of Node's flavours exist on every host: a bare `path` is the host's flavour (`path.win32` on Windows — backslashes, `;`, drive letters, UNC roots — and `path.posix` on Linux/macOS), while `path.posix.*` and `path.win32.*` name one explicitly. join, resolve, dirname, basename, extname, parse/format and isAbsolute follow real Node's algorithms in both flavours, including its asymmetric `basename` extension-stripping rule; the win32 flavour is a C port of Node's own `lib/path.js`.

Exact Works, with caveats Not supported

path.join(...segments)

Works, with caveats
join(...segments: string[]): string

Joins segments with the flavour's separator, then normalizes the result: collapses repeated separators, drops `.` segments, and resolves `..` against segments already seen in the same call. The win32 flavour accepts mixed `/` and `\`, keeps UNC and drive roots intact, and never lets `..` escape a drive root.

Returns

string — The joined, normalized path.

console.log(path.join('a', 'b', 'c'))          // a/b/c
console.log(path.join('/a', 'b', '../c'))      // /a/c
console.log(path.join('a//b///c'))             // a/b/c
See it in a full example →
⚠ Differences from standard
  • A non-string argument held in any is converted to a string at the call instead of throwing ERR_INVALID_ARG_TYPE

path.resolve(...segments)

Exact
resolve(...segments: string[]): string

Absolute-always: starts from `process.cwd()` and keeps only the last absolute segment and what follows it, as in real Node. The win32 flavour also tracks the drive: a segment rooted on another drive wins, a drive-relative segment (`C:x`) resolves against that drive's own current directory, and a rooted-but-driveless segment (`\abs`) takes `cwd`'s drive.

Returns

string — An absolute path.

console.log(path.resolve('/foo', 'bar', 'baz'))  // /foo/bar/baz
console.log(path.resolve('/foo', '/bar', 'baz')) // /bar/baz
See it in a full example →

path.dirname(p)

Exact
dirname(p: string): string

Directory portion of a path; trailing slashes are trimmed first.

Returns

string — The directory portion.

console.log(path.dirname('/foo/bar/baz.js'))   // /foo/bar
See it in a full example →

path.basename(p, ext?)

Exact
basename(p: string, ext?: string): string

Final path segment, optionally with a trailing `ext` stripped — not stripped when doing so would consume the whole segment, unless the entire `path` argument equals `ext` (then returns `""`), matching real Node's own asymmetric rule here.

Returns

string — The final segment.

console.log(path.basename('/foo/bar/baz.js'))  // baz.js
console.log(path.basename('/foo/bar/baz.js', '.js')) // baz
See it in a full example →

path.extname(p)

Exact
extname(p: string): string

Extension of the basename, including the leading `.`; returns `""` for a dotfile whose only `.` is its first character.

Returns

string — The extension, with leading dot.

console.log(path.extname('/foo/bar/baz.js'))   // .js
See it in a full example →

path.parse(p) / path.format(obj)

Exact
parse(p: string): { root, dir, base, ext, name }; format(obj): string

`parse` returns `{root, dir, base, ext, name}`; `format` is the inverse (`base` wins over `name`+`ext`, `dir` falls back to `root`).

Returns

object | string — A parsed path object, or a reassembled string.

const parsed = path.parse('/home/user/dir/file.txt')
console.log(parsed.dir)  // /home/user/dir
console.log(path.format(parsed)) // /home/user/dir/file.txt
See it in a full example →

path.isAbsolute(p)

Exact
isAbsolute(p: string): boolean

True when the path is absolute: `p[0] === '/'` for posix; for win32 a leading `/` or `\` (including UNC) or a drive letter followed by a separator, so drive-relative `C:x` is not absolute.

Returns

boolean — Whether the path is absolute.

console.log(path.isAbsolute('/foo/bar')) // true
console.log(path.isAbsolute('foo/bar'))  // false
See it in a full example →

path.sep / path.delimiter

Exact
path.sep: string; path.delimiter: string

The path-segment separator and the PATH-style delimiter, as compile-time constants for the host's flavour: `/` and `:` on Linux/macOS, `\` and `;` on Windows.

console.log(path.sep)       // / (\ on Windows)
console.log(path.delimiter) // : (; on Windows)
console.log(path.win32.sep) // \ on every host
See it in a full example →

path.posix / path.win32

Works, with caveats
path.posix: PathFlavour; path.win32: PathFlavour

The two flavour objects Node exposes on every platform. Each carries the same method set as `path` — join, resolve, dirname, basename, extname, isAbsolute, parse, format, sep, delimiter — with posix or Windows semantics regardless of the host. A bare `path` dispatches to the host's flavour.

console.log(path.win32.join('C:\\Users', 'me', '..', 'you')) // C:\Users\you
console.log(path.win32.resolve('C:\\home', 'D:\\x', 'y'))    // D:\x\y
console.log(path.posix.join('/a', 'b', '../c'))            // /a/c
See it in a full example →
⚠ Differences from standard
  • matchesGlob is missing from both flavours.

path.normalize(p) / path.relative(from, to) / path.toNamespacedPath(p)

Exact
normalize(p: string): string; relative(from: string, to: string): string; toNamespacedPath(p: string): string

`normalize` resolves `.`/`..` and repeated separators and keeps a trailing separator. `relative` resolves both ends against `process.cwd()` and walks up with `..`; the win32 flavour compares case-insensitively and returns `to` outright across drives or UNC shares. `toNamespacedPath` is the identity for posix and the resolved `\\?\C:\…` / `\\?\UNC\…` long-path form for win32. All three exist in both flavours.

Returns

string — The normalized, relative, or namespaced path.

console.log(path.posix.normalize('/a/../b/./c/'))          // /b/c/
console.log(path.posix.relative('/a/b', '/a/c'))           // ../c
console.log(path.win32.relative('C:\\a\\b\\c\\d', 'C:\\a\\b')) // ..\..
console.log(path.win32.toNamespacedPath('\\\\srv\\sh\\x')) // \\?\UNC\srv\sh\x
See it in a full example →