EN
Node.js - check if path is absolute
0 points
In this article, we would like to show you how to check if the path is absolute in Node.js.
1. Import path
module using:
xxxxxxxxxx
1
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:
xxxxxxxxxx
1
const path = require('path');
2
3
// example paths
4
const absolutePath = 'C:/projects/app/index.js';
5
const relativePath = './index.js';
6
7
// check if path is absolute
8
const result1 = path.isAbsolute(absolutePath);
9
const result2 = path.isAbsolute(relativePath);
10
11
console.log(result1); // true
12
console.log(result2); // false
Output:
xxxxxxxxxx
1
true
2
false