• Node.js Video Tutorials

Node.js - path.resolve() Method



The Node.js path.resolve() method of the path module rectifies a series of path segments; or paths to form an absolute path. The given path segments are processed in right to left direction, with each subsequent path prepended until an absolute path is formed.

This method will use the current working directory when there is no absolute path formed, even after processing all given path segments. This method will ignore the zero-length path segments. The absolute path of the current working directory will be returned if no path segments are passed by this method.

Syntax

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

path.resolve([...paths])

Parameters

  • …paths − This parameter holds a sequence of path segments that would be resolved to form a complete absolute path.

Return value

This method will return a string that represents an absolute path.

Example

The Node.js path.resolve() method will resolve the sequence of given paths from right to left, prepending each of the paths until an absolute path is created.

In the following example, we are passing path segments that are not absolute.

const path = require('path');
path1 = path.resolve('CSS/', "..", 'HTML/', "..", 'file.html')
console.log(path1);

Output

After executing the above program in online compiler (POSIX), the path.resolve() method resolves the path segments from right to left and returned an absolute path.

/home/cg/root/63a02f1595a95/file.html

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

C:\Users\Lenovo\Desktop\JavaScript\file.html

Example

If no path segments are passed to the path.resolve() method, it will return the absolute path of the current working directory.

In the following program, we are passing an empty string to the path parameter.

const path = require('path');
path1 = path.resolve('')
console.log(path1);

Output

After executing the above program in online compiler (POSIX), the path.resolve() method will return the absolute path of the current working directory.

/home/cg/root/63a02f1595a95

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

C:\Users\Lenovo\Desktop\JavaScript

Example

If the first path, segment is not treated as the root segment, then path.resolve() method will assign the current working directory to the path segments passed.

In the program below, we are not treating the first segment as a root segment.

const path = require('path');
path1 = path.resolve('CSS/', 'HTML/', 'file.html')
console.log(path1);

Output

If we compile and run the above program in online compiler (POSIX), the path.resolve() will return the given path segments attached to the current working directory.

/home/cg/root/63a02f1595a95/CSS/HTML/file.html

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

C:\Users\Lenovo\Desktop\JavaScript\CSS\HTML\file.html

Example

If we pass a value that is not a string type to the …paths parameter, the method will throw a TypeError.

In the given program, we are passing an instance of an array instead of a string to the …paths parameter of the path.resolve() method.

const path = require('path');
path1 = path.resolve(['/file'])
console.log(path1);

TypeError

If we compile and run the above program, the path.relative() method throws a TypeError as the …paths parameter is not a string.

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.resolve (path.js:1090:7)
   at Object.<anonymous> (/home/cg/root/63a02f1595a95/main.js:3:14)
   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