path.relative()
Returns the relative path from one location to another.
Syntax
path.relative(from, to) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
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.