Node.js Reference ยท fs and fs/promises
Node.js fs.access() method
Reference for fs.access() in Node.js: test whether a path exists and can be read, written or executed, with R_OK, W_OK and X_OK, and why it can be racy.
fs.access(path[, mode], callback) asks the operating system whether the current process may access path in the way described by mode. If the check passes, the callback gets no error; if not, it gets an error whose code says why (ENOENT for missing, EACCES for denied).
The default mode is fs.constants.F_OK, which only tests that the path exists. Combine R_OK, W_OK and X_OK with bitwise OR to test several permissions at once. In fs/promises, access() resolves with undefined on success and rejects otherwise, which makes it the usual way to write an "exists" helper.
import { accessSync, constants } from 'node:fs'; accessSync('data.csv', constants.R_OK); // throws if not readable
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
fs.access(path[, mode], callback) fs.accessSync(path[, mode]): undefined fsPromises.access(path[, mode]): Promise<undefined> // path: string | Buffer | URL // mode: integer, default fs.constants.F_OK // callback: (err: Error | null) => void
| Constant | Value | Meaning |
|---|---|---|
F_OK |
0 | The path exists (default). |
R_OK |
4 | The process can read it. |
W_OK |
2 | The process can write it. |
X_OK |
1 | The process can execute it (or, for a directory, search it). On Windows it behaves like F_OK. |
Returns: nothing. Success is the absence of an error.
Errors: ENOENT (missing), EACCES (permission denied), EPERM, ENOTDIR (a path component is not a directory), ELOOP (too many symlinks), ERR_INVALID_ARG_TYPE.
Usage notes
- The result describes the moment of the call. Another process can change permissions or delete the file straight afterwards. Do not use
access()to "guard" a later open; open the file and handle the error. - Real versus effective IDs: the check uses the process's real user and group IDs, which can differ from the effective IDs in setuid programs.
- Root passes almost everything. Running as
root,R_OKandW_OKsucceed on any existing file, andX_OKsucceeds if any execute bit is set. - Windows only really understands "exists" and "read-only".
W_OKfails on read-only files;X_OKis the same asF_OK. - Directories:
X_OKmeans you may enter and look up names in it.W_OKmeans you may create or delete entries. - Symlinks are followed. A dangling link fails with
ENOENT. - Good uses: early, friendly checks in CLIs ("cannot read input file"), and the
exists()helper shown above. PreferexistsSync()for a simple synchronous yes/no.