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
| Name | Type | Description |
|---|---|---|
| from | string | Starting location. |
| to | string | Destination 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')]));