Node.js Reference ยท process
Node.js process.umask() method
Reference for process.umask() in Node.js: read or change the file mode creation mask, how it affects new files and directories, and why the no-argument form is deprecated.
process.umask() reads or changes the file mode creation mask of the current process. The mask is a set of permission bits that are removed from the mode of every file or directory the process creates. A mask of 0o022 removes write permission for the group and for others, so a file that would be created with mode 0o666 ends up as 0o644.
Calling it with a new mask sets the mask and returns the previous one. Because the mask belongs to the whole process, a change affects every later fs.writeFile(), fs.mkdir() and fs.open() call that creates something, including calls made by libraries.
const previous = process.umask(0o077); // new files are now created with mode 0o600 (0o666 minus 0o077) process.umask(previous); // restore
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
process.umask(): number // deprecated form process.umask(mask: number | string): number
| Name | Description |
|---|---|
mask |
The new mask. A number (usually written as an octal literal such as 0o022) or a string of octal digits such as '022'. |
Returns: the previous mask as a number.
Errors / edge cases:
- A string that is not valid octal, or a value outside the permitted range, throws an
ERR_INVALID_ARG_VALUEorERR_INVALID_ARG_TYPEerror. - In a worker thread, calling it with an argument throws
ERR_WORKER_UNSUPPORTED_OPERATION, because the mask is shared by the whole process. Reading it is still allowed there. - On Windows only the owner write bit has any meaning, so the function exists but has little practical effect.
Usage notes
- The no-argument form is deprecated. On POSIX systems the only way to read the mask is to set one and put it back, which is not safe when threads create files at the same moment. Prefer to set the mask once at startup, or avoid depending on it and pass an explicit
modeoption. - It only removes bits. The mask cannot add permissions. A file asked to be created with
0o600stays0o600under any mask that does not clear owner bits. - Default modes.
fs.writeFile()andfs.open()request0o666, andfs.mkdir()requests0o777. Executable bits are never added to regular files by the mask. - Explicit modes are masked too.
fs.writeFile(path, data, { mode: 0o666 })is still reduced by the umask. To set an exact mode regardless of the mask, create the file and then callfs.chmod(). - Global state. Change the mask only around the code that needs it and restore it, or do it once at the top of a program. Never leave it changed inside a library.
- Existing files are not affected. Use
fs.chmod()for those. - Typical values.
0o022(the common default) gives644files and755directories;0o077keeps everything private to the owner, which suits tools that write secrets.