Skip to content
Tutorials Center
My learning

Topics

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.

NODE
import path from "node:path";

path.isAbsolute("/srv/app");   // true
path.isAbsolute("src/app");    // false

Try it

Node.js POSIX rules

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.

Node.js Windows rules and a practical helper

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

NODE
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 ~/notes is 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 as C:foo is not absolute, because it depends on the current directory of drive C.
  • Rooted does not mean fully qualified. On Windows, \temp is "absolute" in Node's sense but still depends on the current drive.
  • It does not check the disk. Use fs.access() or fs.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.isAbsolute follows the platform running your code. Use path.posix.isAbsolute or path.win32.isAbsolute to apply one set of rules everywhere, for example when validating paths from a configuration made on another system.