Node.js path.relative() Method
Last Updated :
13 Oct, 2021
Improve
The path.relative() method is used to find the relative path from a given path to another path based on the current working directory. If both the given paths are the same, it would resolve to a zero-length string.
Syntax:
javascript
Output:
path.relative( from, to )Parameters: This method accept two parameters as mentioned above and described below:
- from: It is the file path that would be used as base path.
- to: It is the file path that would be used to find the relative path.
// Node.js program to demonstrate the
// path.relative() method
// Import the path module
const path = require('path');
path1 = path.relative("geeks/website", "geeks/index.html");
console.log(path1)
path2 = path.relative("users/admin", "admin/files/website");
console.log(path2)
// When both the paths are same
// It returns blank string
path3 = path.relative("users/admin", "users/admin");
console.log(path3)
..\index.html ..\..\admin\files\websiteReference: https://nodejs.org/api/path.html#path_path_relative_from_to