path
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`.
path.join(...segments)
Works, with caveatsJoins 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.
Returnsstring — 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- A non-string argument held in
anyis converted to a string at the call instead of throwingERR_INVALID_ARG_TYPE
path.resolve(...segments)
ExactAbsolute-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.
Returnsstring — An absolute path.
console.log(path.resolve('/foo', 'bar', 'baz')) // /foo/bar/baz
console.log(path.resolve('/foo', '/bar', 'baz')) // /bar/bazpath.dirname(p)
ExactDirectory portion of a path; trailing slashes are trimmed first.
Returnsstring — The directory portion.
console.log(path.dirname('/foo/bar/baz.js')) // /foo/barpath.basename(p, ext?)
ExactFinal 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.
Returnsstring — The final segment.
console.log(path.basename('/foo/bar/baz.js')) // baz.js
console.log(path.basename('/foo/bar/baz.js', '.js')) // bazpath.extname(p)
ExactExtension of the basename, including the leading `.`; returns `""` for a dotfile whose only `.` is its first character.
Returnsstring — The extension, with leading dot.
console.log(path.extname('/foo/bar/baz.js')) // .jspath.parse(p) / path.format(obj)
Exact`parse` returns `{root, dir, base, ext, name}`; `format` is the inverse (`base` wins over `name`+`ext`, `dir` falls back to `root`).
Returnsobject | 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.txtpath.isAbsolute(p)
ExactTrue 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.
Returnsboolean — Whether the path is absolute.
console.log(path.isAbsolute('/foo/bar')) // true
console.log(path.isAbsolute('foo/bar')) // falsepath.sep / path.delimiter
ExactThe 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 hostpath.posix / path.win32
Works, with caveatsThe 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/cmatchesGlobis missing from both flavours.
path.normalize(p) / path.relative(from, to) / path.toNamespacedPath(p)
Exact`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.
Returnsstring — 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