fs.existsSync() / access()

Checks whether a path exists on the filesystem.

Since Node 0.x; access promises Node 11.1 Spec ↗

Syntax

existsSync(path)  /  await access(path[, mode])

Parameters

NameTypeRequiredDescription
path string | Buffer | URL Yes The path to test.
mode integer No Optional accessibility check for `access`, e.g. `constants.R_OK` or `constants.W_OK`.

Returns

boolean | Promise<void> — existsSync returns a boolean; access resolves or rejects.

Examples

import { existsSync } from 'node:fs';

console.log(existsSync('./package.json'));
Output
true
import { access, constants } from 'node:fs/promises';

try {
  await access('./config.json', constants.R_OK);
  console.log('readable');
} catch {
  console.log('missing or no permission');
}
Output
readable

Notes

The async `fs.exists` callback is deprecated. Avoid check-then-use patterns: the file can change between the check and the operation (TOCTOU). Prefer just attempting the operation and handling `ENOENT`.

See also