Path construction
Constructing filesystem paths from directory and file components.
Node.js · Module
Node.js's node:path module provides platform-aware utilities for manipulating file and directory path strings. It supports construction, normalization, inspection, absolute resolution, relative-path calculation, and explicit POSIX or Windows behavior.
The node:path module offers string-based utilities for constructing, normalizing, inspecting, resolving, and comparing file and directory paths. Its default methods follow the host platform; use path.posix or path.win32 when deterministic platform-specific behavior is required.
const path = require('node:path');
import path from 'node:path';| Name | Type | Description |
|---|---|---|
method arguments | string | ...string[] | Path segments or path strings supplied to methods such as join(), resolve(), and relative(); exact parameters depend on the selected method. |
Individual path utility methods return strings, booleans, or parsed/ formatted path objects according to the selected method.
node:path with either require() or an ES module import.basename() extracts the final component of a path, commonly the filename.extname() identifies the extension beginning at the final dot in the path's last component.join() combines segments and normalizes the result using the host platform's separator.relative() to compute a path that describes navigation from one path to another.path.posix when path syntax must remain POSIX-specific regardless of the host operating system.import path from 'node:path';
const file = path.join('reports', '2025', 'summary.txt');
console.log(path.basename(file));
console.log(path.extname(file));
console.log(path.dirname(file));Builds a normalized platform-specific path, then extracts its final name, extension, and containing directory.
Constructing filesystem paths from directory and file components.
Extracting filenames, extensions, or parent directories from path strings.
Generating portable paths with explicit POSIX or Windows semantics.
node:path module?The module supplies utilities for working with file and directory paths. The default implementation uses the conventions of the operating system running Node.js, and it can be imported with either CommonJS or ES module syntax.
Follow-up: How would you process POSIX paths consistently on Windows? Use path.posix, which exposes the POSIX-specific implementations.
path.join() do?path.join() combines path segments with the platform-specific separator and normalizes the resulting path. It is useful for constructing a path from pieces without manually inserting separators.
Follow-up: How does resolve() differ? It produces an absolute path, using the current working directory when the supplied segments do not establish one.
path.relative(from, to) returns the relative path from one location to another, based on the current working directory. It performs path computation rather than checking the filesystem.
Follow-up: What should be used when the required result must be absolute? Use path.resolve() to resolve a sequence of segments into an absolute path.
path.basename()path.dirname()path.extname()path.format()path.isAbsolute()path.join()path.normalize()path.parse()