EN
TypeScript / 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. Install node types with the following command:
xxxxxxxxxx
1
npm install --save @types/node
2. Import path
module using:
xxxxxxxxxx
1
import * as path from 'path';
3. 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
).
xxxxxxxxxx
1
import * as path from 'path';
2
3
// example paths
4
const absolutePath: string = 'C:/projects/app/index.js';
5
const relativePath: string = './index.js';
6
7
// check if path is absolute
8
const result1: boolean = path.isAbsolute(absolutePath);
9
const result2: boolean = path.isAbsolute(relativePath);
10
11
console.log(result1); // true
12
console.log(result2); // false
Output:
xxxxxxxxxx
1
true
2
false