path.isAbsolute()
Determines whether a path is an absolute path.
Syntax
path.isAbsolute(path) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Yes | The path to test. |
Returns
boolean — true if the path is absolute.
Examples
import { isAbsolute } from 'node:path';
console.log(isAbsolute('/etc/hosts'));
console.log(isAbsolute('src/index.js'));
Output
true
false
import { isAbsolute, resolve } from 'node:path';
const input = 'data/file.txt';
const full = isAbsolute(input) ? input : resolve(input);
console.log(full);
Output
/home/user/project/data/file.txt
Notes
POSIX treats a leading `/` as absolute; Windows also accepts drive
letters and UNC paths. An empty string is not absolute.