Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท fs and fs/promises

Node.js fs.chmod() method

Reference for fs.chmod() in Node.js: set permission bits with octal numbers or strings, read them back with stat, Windows limits, fchmod and symlinks.

fs.chmod(path, mode, callback) changes the permission bits of a file or directory: who may read, write or execute it. The mode is normally written as an octal number, so 0o644 means "owner can read and write, everyone else can read".

Unlike the mode option of fs.writeFile() or fs.mkdir(), which is trimmed by the process umask, chmod sets the bits exactly. That makes it the right call when you need a precise result, such as a private key file with 0o600 or a script with 0o755.

NODE
import fs from "node:fs";

fs.chmodSync("deploy.sh", 0o755);

Try it

Node.js Setting and reading permissions

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 An open file and the errors

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
fs.chmod(path, mode, callback)
fs.chmodSync(path, mode): undefined
fsPromises.chmod(path, mode): Promise<undefined>

fs.fchmod(fd, mode, callback)      // also fs.fchmodSync, filehandle.chmod(mode)
Name Description
path A string, Buffer or URL. Symbolic links are followed, so the target changes.
mode An integer (use octal such as 0o644) or a string of octal digits ("644").
fd An open file descriptor, for fchmod.

Returns: undefined.

Errors: ENOENT if the path does not exist, EPERM if you do not own the file (and are not root), EROFS on a read-only file system, ERR_INVALID_ARG_VALUE or ERR_OUT_OF_RANGE for an invalid mode.

Usage notes

  • Use octal notation. 0o644 is 420 in decimal; writing 644 as a number sets nonsense bits. A string "644" is parsed as octal, which is why strings work.
  • The nine bits are read (4), write (2) and execute (1) for owner, group and others. Directories need execute to be entered.
  • Only the owner or root may change a mode. Other users get EPERM.
  • Windows has no Unix modes. Only the "write" bit matters: removing it makes the file read-only. 0o666 and 0o444 are the useful values there.
  • Combine bits with | and & after reading the current mode with stat, rather than overwriting everything.
  • Symlinks: chmod follows them. fs.lchmod() exists only on macOS, and is deprecated; on Linux the permissions of a link itself are not meaningful.
  • Files created with a mode option go through the umask. A later chmod is the reliable way to get an exact value.
  • Set the bits on creation when secrecy matters: pass mode: 0o600 to writeFile so the file is never briefly world-readable, then chmod if you must be exact.