• Node.js Video Tutorials

Node.js - path.isAbsolute() Method



The Node.js path.isAbsolute() method will return a Boolean value, if the given path is absolute or not. A path that has all of the details required to locate a particular file is considered an absolute path. It is used to determine if a given path resolves to the root of the file system.

In case the given file path is zero length, or an empty string, this method will return false. If any value is given instead of a file path string, this method will return a TypeError.

Syntax

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

path.isAbsolute(path)

Parameters

  • path − This parameter specifies a particular file path string; that is used to determine whether it is an absolute path or not. A TypeError will be thrown, if the path is not a string.

Return value

This method will return Boolean. If the given file path is absolute, then it returns true, if not it returns false.

Example

The Node.js path.isAbsolute() method will return true if the given path is absolute.

In the following example, we are trying to perform all the possibilities of the given path file being absolute.

const path = require('path');

var path1 = path.isAbsolute("/Users/Lenovo/Desktop/JavaScript/Nodefile.js");
console.log(path1);

var path2 = path.isAbsolute("////////Nodefile.js");
console.log(path2);

var path3 = path.isAbsolute("/Users/Lenovo/...");
console.log(path3);

Output

After executing the above program, the path.isAbsolute() method returns true because the given file path is absolute.

true
true
true

Example

The Node.js path.isAbsolute() method will return false if the given path is an absolute path.

In the following example, we are trying to perform all the possibilities of the given path file which will return false.

const path = require('path');

var path1 = path.isAbsolute("Users//Lenovo/Desktop/JavaScript/Nodefile.js");
console.log(path1);

var path2 = path.isAbsolute("Users\\Lenovo\Desktop");
console.log(path2);

var path3 = path.isAbsolute(".");
console.log(path3);

var path4 = path.isAbsolute("");
console.log(path4)

Output

As we can see in the output below, the method returns false because the given file path is NOT absolute.

false
false
false
false

Example

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

In the following example, we are passing an integer instead of a string to the path parameter of the path.isAbsolute() method.

const path = require('path');
var path1 = path.isAbsolute(98765467);
console.log(path1);

TypeError

If we compile and run the above program, this method throws a TypeError as the path parameter is not a string value.

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 number
   at assertPath (path.js:39:11)
   at Object.isAbsolute (path.js:1146:5)
   at Object.<anonymous> (/home/cg/root/63a028ab0650d/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