Languages
[Edit]
EN

Node.js - check if path is absolute

0 points
Created by:
mkrieger1
516

In this article, we would like to show you how to check if path is absolute in Node.js.

1. Import path module using:

const path = require('path');

2. Use path.isAbsolute() method with the path you want to check as an argument to find out if it is absolute (true) or relative (false).

Practical example:

const path = require('path');

// example paths
const absolutePath = 'C:/projects/app/index.js';
const relativePath = './index.js';

// check if path is absolute
const result1 = path.isAbsolute(absolutePath);
const result2 = path.isAbsolute(relativePath);

console.log(result1); // true
console.log(result2); // false

Output:

true
false
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join