Node.js Reference ยท fs and fs/promises
Node.js fs.constants
Reference for fs.constants in Node.js: F_OK, R_OK, W_OK, X_OK for access checks, O_* open flags, COPYFILE_* options and S_IF* file type masks, with examples.
fs.constants is a frozen object of named integers used by the file system functions: permission checks, open flags, copy options and file type bits. The same object is exported as fs.promises.constants, and the access and copy names are also available directly on fs (for example fs.F_OK in old code).
Most of the time you meet them as arguments: fs.access(path, fs.constants.R_OK) or fs.copyFile(a, b, fs.constants.COPYFILE_EXCL). They are bit flags, so you combine several with the bitwise OR operator | and test one with &. Prefer the names over the numbers, because several values (the O_* family in particular) differ between operating systems.
import fs from "node:fs"; const { R_OK, W_OK } = fs.constants; fs.accessSync("settings.json", R_OK | W_OK); // throws if not readable and writable
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.
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.constants: Readonly<Record<string, number>>
Groups you will actually use:
| Group | Names | Used by |
|---|---|---|
| Existence and access | F_OK (0), R_OK (4), W_OK (2), X_OK (1) |
fs.access() |
| Copy options | COPYFILE_EXCL, COPYFILE_FICLONE, COPYFILE_FICLONE_FORCE |
fs.copyFile() |
| Open flags | O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, O_SYNC, O_DSYNC, O_DIRECTORY, O_NOFOLLOW, O_NOATIME, O_NONBLOCK, O_DIRECT |
fs.open(), flag options |
| File type masks | S_IFMT, S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFIFO, S_IFLNK, S_IFSOCK |
stats.mode |
| Permission bits | S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, and the G and O variants |
stats.mode, chmod |
Which names exist depends on the platform. O_NOATIME and O_DIRECT exist on Linux, for example, while Windows adds a few UV_FS_O_* names.
Usage notes
- Combine with
|, test with&.mode & S_IFMTisolates the file type;mode & 0o777isolates the permission bits. F_OKis 0, soF_OK | R_OKis the same asR_OK. PassF_OKalone to ask only "does it exist?".fs.access()answers for the real user, and the answer can be stale by the time you act. Often it is better to just try the operation and handle the error.COPYFILE_FICLONEtries a copy-on-write clone and silently falls back to a normal copy.COPYFILE_FICLONE_FORCEfails instead of falling back. Not every file system can clone.- Do not hard-code open flag numbers. The values of
O_CREAT,O_EXCLand friends differ between Linux, macOS and Windows. Use the names, or the string flags in File system flags. - Windows ignores most permission bits. Execute checks (
X_OK) succeed for any existing file there, andW_OKonly reflects the read-only attribute. - Prefer
stats.isFile()and the otherfs.Statsmethods over manualS_IFMTmath in application code.