Languages
[Edit]
EN

TypeScript / Node.js - check if path is absolute

0 points
Created by:
Bess
601

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:

npm install --save @types/node

2. Import path module using:

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).

Practical example

import * as path from 'path';

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

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

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

Output:

true
false

See also

  1. Node.js - check if path is absolute (JavaScript solution)  
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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