Node.js Reference ยท fs and fs/promises
Node.js fs.open() and fs.close() methods
Reference for fs.open(), fs.openSync() and fs.close() in Node.js: get a file descriptor, choose flags and mode, and always close it to avoid leaking handles.
fs.open(path[, flags[, mode]], callback) opens a file and gives you a file descriptor: a small integer the operating system uses to identify the open file. You pass that number to low-level functions such as fs.read(), fs.write() and fs.fstat(). When you are done, fs.close(fd, callback) releases it.
Most programs never need descriptors: readFile() and writeFile() open and close the file for you. Use them when you want to read or write at particular positions, keep a file open across many operations, or hold a lock-like handle on it. The promise API offers the same with an object: fsPromises.open() returns a FileHandle.
import { openSync, closeSync } from 'node:fs'; const fd = openSync('data.bin', 'r'); closeSync(fd);
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.open(path[, flags[, mode]], callback) fs.openSync(path[, flags[, mode]]): number fs.close(fd[, callback]) fs.closeSync(fd): undefined // callback for open: (err: Error | null, fd: number) => void // callback for close: (err: Error | null) => void // path: string | Buffer | URL // flags: string | number (default 'r') // mode: string | integer (default 0o666, only used when the file is created)
| Name | Description |
|---|---|
path |
File to open. |
flags |
How to open it: 'r', 'r+', 'w', 'wx', 'a' and so on. See file system flags. Numeric flags from fs.constants also work. |
mode |
Permission bits for a new file (reduced by the umask). |
fd |
The descriptor returned by open. |
Returns: openSync() returns the descriptor. The callback form passes it as the second argument. closeSync() returns undefined.
Errors: ENOENT (missing file with a read flag, or missing directory), EEXIST (flag with x), EACCES, EISDIR, EMFILE (the process has too many open files), EBADF (closing or using an invalid descriptor).
Usage notes
- Always close what you open. Descriptors are a limited resource. A leak shows up much later as
EMFILE: too many open files. In synchronous code usetry/finally. - Closing twice is an error (
EBADF), and worse, the number may by then belong to a different file that was opened in the meantime. Set your variable toundefinedafter closing. - Position: each descriptor has a current position that sequential reads and writes advance. Pass an explicit position to
read/writeto ignore it. - Flags decide everything.
'r'fails if the file is missing and forbids writing;'w'truncates immediately at open time, even if you never write. - Callback is optional for
close()in current versions, but passing one lets you see errors such as an I/O failure while flushing. - Prefer higher-level calls when you simply want the whole content. Use descriptors for random access, tail-reading logs, or implementing your own file formats.
- Promises: with
fsPromises.open()you get aFileHandlethat closes itself when used withawait usingin recent Node.js versions, and that warns if it is garbage collected while still open.