Filesystem
fs/promises.access()
Checks access at one instant. Avoid using it as a precondition for opening a file because state may change before the operation. Tested on Node.js 22.16.0. Example output (one line per entry): ["ENOENT"].
Formal Syntax & Structure
nodejs syntaxnodejs
access(path[, mode])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| path | string | URL | Entry path. |
| mode | number | Access constants. |
Return Value
Promise<void>
Resolves if the check succeeds.
Working Example
Example Snippetnodejs
import * as fs from 'node:fs/promises';import { constants } from 'node:fs';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir = await fs.mkdtemp(join(tmpdir(), 'bytemind-'));try { const p = join(dir, 'book.txt'); await fs.writeFile(p, 'catalog'); await fs.access(p, constants.F_OK); try { await fs.access(join(dir, 'absent')); } catch (e) { console.log(e.code); }}finally { await fs.rm(dir, { recursive: true, force: true });}