Node.js path.relative() Method
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:
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.
Return Value: It returns a string with the normalized form of the path.
Below program illustrates the path.relative() method in Node.js:
Example:
// Node.js program to demonstrate the // path.relative() method // Import the path module const path = require( 'path' ); path1 = path.relative( "Beginner/website" , "Beginner/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) |
Output:
..\index.html ..\..\admin\files\website
Reference: https://nodejs.org/api/path.html#path_path_relative_from_to
Contact Us