Node.js Reference ยท path, os and url
Node.js path.isAbsolute() method
Reference for path.isAbsolute() in Node.js: test whether a path is absolute on POSIX and Windows, including drive letters, UNC paths and the tricky C:foo case.
path.isAbsolute() returns true when a path points to the same place no matter which directory you start from, and false for paths that depend on the current directory. It checks only the text: the path does not have to exist.
It is typically used to decide whether to leave a path alone or to join it onto a base folder.
import path from "node:path"; path.isAbsolute("/srv/app"); // true path.isAbsolute("src/app"); // false
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
path.isAbsolute(path): boolean
| Name | Description |
|---|---|
path |
The path string to test. |
Returns: true if the path is absolute, otherwise false. An empty string is false.
Errors: ERR_INVALID_ARG_TYPE if path is not a string.
Usage notes
- POSIX rule: a path is absolute exactly when it starts with
/. Segments like~are expanded by shells, not by Node, so~/notesis relative. - Windows rule: a path is absolute when it starts with a separator (
\temp,/temp), a drive letter followed by a separator (C:\,C:/) or a UNC prefix (\\server\share). A bare drive reference such asC:foois not absolute, because it depends on the current directory of driveC. - Rooted does not mean fully qualified. On Windows,
\tempis "absolute" in Node's sense but still depends on the current drive. - It does not check the disk. Use
fs.access()orfs.stat()to test existence. - Make a path absolute with
path.resolve(), which always returns an absolute path. - Not a safety check. An absolute path in user input can reach any file the process can read. Reject or confine such input before using it. See Path traversal.
- Platform specific: the plain
path.isAbsolutefollows the platform running your code. Usepath.posix.isAbsoluteorpath.win32.isAbsoluteto apply one set of rules everywhere, for example when validating paths from a configuration made on another system.