Path

path.relative()

Computes a lexical route between two resolved paths. Tested on Node.js 22.16.0. A relative result can contain parent segments; do not treat it alone as a security check.

Formal Syntax & Structure

nodejs syntaxnodejs
path.relative(from, to)

Parameters & Attributes

NameTypeDescription
fromstringStarting location.
tostringDestination location.

Return Value

string

Relative path or empty string for identical resolved locations.

Working Example

Example Snippetnodejs
import path from 'node:path';console.log(JSON.stringify([path.posix.relative('/catalog/a','/catalog/b'),path.posix.relative('/catalog','/catalog')]));
Node.js Reference