path.relative()

Returns the relative path from one location to another.

Since Node 0.x Spec ↗

Syntax

path.relative(from, to)

Parameters

NameTypeRequiredDescription
from string Yes The starting path.
to string Yes The destination path.

Returns

string — The relative path from `from` to `to`.

Examples

import { relative } from 'node:path';

console.log(relative('/app/src', '/app/test/unit'));
Output
../test/unit
import { relative } from 'node:path';

console.log(relative('/app/src', '/app/src/lib/x.js'));
Output
lib/x.js

Notes

Both arguments are resolved against `process.cwd()` first if not absolute. If `from` equals `to` the result is an empty string. Handy for printing user-friendly paths in CLI output.

See also