Portable path construction
Construct paths from directory and file components without manually inserting separators.
Node.js · Module
The node:path module provides utilities for manipulating file and directory path strings. Its default methods follow the host platform, while path.posix and path.win32 provide explicit platform-specific behavior.
The module exposes platform-aware utilities for joining, normalizing, parsing, and comparing file and directory paths. Use the default API for the host operating system, or select path.posix or path.win32 when a specific path convention is required.
const path = require('node:path');
import path from 'node:path';| Name | Type | Description |
|---|---|---|
path or segments | string | A path string or path segments, depending on the selected node:path method. |
The module exports path utilities; individual methods return path strings or path-derived values according to the method used.
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 platform-aware path, then extracts its final component, extension, and containing directory.
Construct paths from directory and file components without manually inserting separators.
Extract filenames, directories, or extensions from user- or program-generated paths.
Read or generate paths in a format different from the current operating system.
The default path methods use the conventions of the operating system running the process. For portable handling of a known format, use path.posix or path.win32 explicitly.
Follow-up: How would you parse a Windows path on Linux? Use path.win32.parse or another path.win32 method rather than the host-default API.
path.join combines path segments with the platform separator and normalizes the result. It is intended for constructing paths from components rather than manually concatenating separator characters.
Follow-up: What should be used when the desired separator convention is not the host platform? Use path.posix.join or path.win32.join.
path.basename returns the last portion of a path, while path.dirname returns the directory portion. path.extname returns the extension from the last period in the last portion of the path.
Follow-up: What happens with trailing separators in basename input? They are ignored when identifying the final portion.
path.basename()path.dirname()path.extname()path.format()path.join()path.parse()path.posixpath.win32