
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
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)