Node.js path.isAbsolute() Method

Last Updated : 4 Aug, 2026

The path.isAbsolute() is used to determine whether a path is specified as an absolute path. It helps distinguish complete file or directory locations from paths that depend on the current working directory, making path validation and file system operations more reliable.

  • Identifies whether a path is absolute or relative.
  • Useful for validating user-provided file paths.
  • Helps prevent incorrect path handling in file system operations.

Syntax:

path.isAbsolute( path )

where,

  • path: The path string to check (TypeError is thrown if this parameter is not a string).

Return Value: Boolean value indicating whether the path is an absolute path. It returns 'false' if the path is of zero-length.

Example 1:

javascript
// Node.js program to demonstrate the   
// path.isAbsolute() method

// Import the path module
const path = require('path');
 
path1 = path.isAbsolute("/user/bash/");
console.log(path1);
 
path2 = path.isAbsolute("user/bash/readme.md");
console.log(path2);
 
path3 = path.isAbsolute("/user/bash/readme.md");
console.log(path3);
 
path4 = path.isAbsolute("..");
console.log(path4);

In this code,

  • path.isAbsolute() checks whether /user/bash/ is an absolute path.
  • It then checks a relative path (user/bash/readme.md) and an absolute file path (/user/bash/readme.md).
  • Finally, it checks .. to determine whether the parent-directory reference is an absolute path.

Output:

true
false
true
false

Example 2:

javascript
// Node.js program to demonstrate the   
// path.isAbsolute() method

// Import the path module
const path = require('path');
 
path1 = path.isAbsolute("\\user\\bash\\");
console.log(path1);
 
path2 = path.isAbsolute("user\\bash\\readme.md");
console.log(path2);
 
path3 = path.isAbsolute("\\user\\bash\\readme.md");
console.log(path3);
 
path4 = path.isAbsolute("..");
console.log(path4);

In this code,

  • path.isAbsolute() checks whether the Windows-style path \user\bash\ is absolute.
  • It then checks a relative file path and an absolute file path using backslashes.
  • Finally, it checks whether .. represents an absolute path.

Output:

true
false
true
false

Use Cases of path.isAbsolute()

  • Path validation: Checks whether a path is absolute before processing it.
  • File system operations: Ensures a path points to a fixed location rather than depending on the current working directory.
  • User input handling: Verifies whether user-provided paths are absolute or relative.
  • Path resolution: Determines if a path needs to be converted to an absolute path before use.

Reference: https://nodejs.org/api/path.html#path_path_isabsolute_path

Comment

Explore