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

Node.js · Module

path

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.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
method argumentsstring | ...string[]Path segments or path strings supplied to methods such as join(), resolve(), and relative(); exact parameters depend on the selected method.

Returns

Individual path utility methods return strings, booleans, or parsed/ formatted path objects according to the selected method.

Key points

  • Use this built-in module whenever application logic needs to manipulate filesystem path strings.
  • Import it as 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.
  • Use relative() to compute a path that describes navigation from one path to another.
  • Select path.posix when path syntax must remain POSIX-specific regardless of the host operating system.

Common mistakes

  • Methods such as basename and dirname disregard trailing separators, which can affect the returned final component or directory.
  • Do not rely on the default path behavior for portable POSIX-format data; select the POSIX implementation explicitly.

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 normalized platform-specific path, then extracts its final name, extension, and containing directory.

When to use it

Path construction

Constructing filesystem paths from directory and file components.

Path inspection

Extracting filenames, extensions, or parent directories from path strings.

Cross-platform path handling

Generating portable paths with explicit POSIX or Windows semantics.

Interview questions

QWhat is the purpose of Node.js's 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.

QWhat does 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.

QHow can a relative path between two paths be calculated?

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()

Source: Path. Checked 2026-10-15.