• Node.js Video Tutorials

Node.js - path.relative() method



There are various operating system-related utility methods and properties given in the Node.js path module.

The Node.js path.relative() method of the path module returns a relative path from one described path (from) to another path (to) based on the current working directory. A zero-length string is returned, when from and to each resolve to the same path (after calling path.resolve() on each). If any zero-length strings are passed to the paths, then this method prints a zero-length string. A TypeError will be thrown if either described path is not a string.

Syntax

Following is the syntax of the Node.js path.relative() method of path module −

path.relative(from, to)

Parameters

  • from − This parameter holds a string file path that represents a base path.

  • to − This parameter is a string file path that would be used to find the relative path from the base path.

Return value

This method returns the relative path from one specified path from to another specified path to.

Example

The path.relative() method will return the relative path if there is a relative path between the from and to parameters.

In the following example, we are providing the paths to both the from and to parameters of the method.

const path = require('path');

var path1 = path.relative('C:/Users/Lenovo/Desktop/JavaScript/nodefile.js', 'C:/Users/Lenovo/Desktop/JavaScript/file/bose.html')

console.log(path1);

Output

Following is the output, when we execute the above code in online compiler (POSIX).

../file/bose.html

Following is the output, when we execute the above code on WINDOWS operating system.

..\file\bose.html

Example

If there is no relative path between from and to paths, the path.relative() method will return the complete to path.

In the below program, we are passing different file paths to from and to parameters of the path.relative() method.

const path = require('path');
var path1 = path.relative('C:/Users/Lenovo/Desktop/JavaScript/nodefile.js', "E:/programs/file")
console.log(path1);

Output

After executing the above program in online compiler (POSIX), the path.relative() method returns the complete 'to' path to the output because there is no relative path.

../../../../../../E:/programs/file

Following is the output, when we execute the above code on WINDOWS operating system.

E:\programs\file

Example

The path.relative() method will return a zero-length string if both the from and to paths are zero-length strings.

In the following example, we are passing empty strings to the from and to parameters of the path.relative() method.

const path = require('path');
var path1 = path.relative('', '');
console.log(path1);

Output

If we compile and run the above program, the path.relative() method returns a zero-length string to the output as there are no paths given.

''

Example

If we pass a value that is not a string type to the from and to parameters, the path.relative() method will throw a TypeError.

In the given example, we are passing an instance of an object, instead of a string to both from and to parameters of the method.

const path = require('path');
var path1 = path.relative({}, {});
console.log(path1);

TypeError

As we can see in the output below, the path.relative() method throws a TypeError as the from and to parameters are not a string values.

path.js:39
   throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
   ^
   
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
   at assertPath (path.js:39:11)
   at Object.relative (path.js:1172:5)
   at Object.<anonymous> (/home/cg/root/63a02f1595a95/main.js:3:18)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
nodejs_path_module.htm
Advertisements