Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js path.relative() Method

  • Last Updated : 13 Oct, 2021

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.


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:


// 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");
path2 = path.relative("users/admin", "admin/files/website");
// When both the paths are same
// It returns blank string
path3 = path.relative("users/admin", "users/admin");




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!