Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

node:path

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.

How it works

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.

Syntax

const path = require('node:path');
import path from 'node:path';

Parameters

NameTypeDescription
path or segmentsstringA path string or path segments, depending on the selected node:path method.

Returns

The module exports path utilities; individual methods return path strings or path-derived values according to the method used.

Key points

  • Import node:path when application code needs to manipulate filesystem path strings.
  • The module supports the node:path specifier in both CommonJS and ES module code.
  • basename extracts the final component of a path.
  • extname identifies the suffix beginning at the final period in the final path component.
  • format constructs a path string from parsed path fields.
  • path.posix exposes implementations that consistently apply POSIX path rules.

Common mistakes

  • The default implementation follows the host platform, so use path.win32 when processing Windows paths regardless of where the code runs.
  • Use path.posix when the path format must remain POSIX-style across operating systems.
  • basename does not preserve trailing directory separators when determining the final path portion.

Example: Build and inspect a file path

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.

When to use it

Portable path construction

Construct paths from directory and file components without manually inserting separators.

Path inspection

Extract filenames, directories, or extensions from user- or program-generated paths.

Cross-platform path handling

Read or generate paths in a format different from the current operating system.

Interview questions

QHow does node:path handle platform differences?

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.

QWhat does path.join do?

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.

QWhich helpers extract parts of a path?

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.posix
  • path.win32

Source: Path. Checked 2026-10-15.