Skip to main content
File access through the fs global. Every operation has two forms: a sync call that blocks the event loop until it completes, and an *Async variant (readFileAsync, writeFileAsync, existsAsync, mkdirAsync, rmAsync, readdirAsync) that returns a Promise, runs on a dedicated 4-thread pool, and rejects with a real Error (e.g. message === "FileNotFound"). Sync calls never throw on I/O errors — a missing file or bad path silently returns undefined. Guard with fs.exists(), or use the *Async form when you want the error. Permission denial is separate: it throws PermissionDenied before the operation runs.

Permissions

fs is deny-by-default.
  • Relative scopes resolve against the launch cwd and stay fixed. A later process.chdir() does not move them.
  • Matching is component-exact: --allow-read=/srv/app covers /srv/app/data.json, not /srv/app2/data.json.
  • Symlinks are resolved before matching. The target must be inside a scope; a symlink inside an allowed directory that points outside is denied. Writes through a dangling symlink are denied.
  • .. traversal, NUL bytes, and paths at or beyond 4096 bytes are denied.
  • Bare --allow-read / --allow-write grant every path.
  • There are no globs or per-extension rules.
Full model: Permissions.

Read a file

Returns the file content as a string (default or "utf8"), or as an ArrayBuffer when "buffer" or "binary" is passed as the second argument. Returns undefined when the I/O fails (missing file, OS permission). Reads cap at 10MB. Without --allow-read on the path, the call throws PermissionDenied instead.

Write a file

Creates the file when missing, overwrites when present. Parent directories must exist — use mkdir first. Needs --allow-write on the file. writeFile and writeFileAsync take string data (UTF-8 text); non-string values such as Uint8Array or ArrayBuffer are coerced via String() rather than written as raw bytes. Writes are capped at 10 MB. Passing data larger than 10 MB throws an error immediately (fs.writeFile: data exceeds the 10 MiB limit (nothing written)) instead of silently truncating the file.

Check existence

Returns true or false. It never throws on a missing path, but a path outside --allow-read throws PermissionDenied — a missing file inside the scope returns false.

Make directories

The second argument is recursive. Pass true to create parents as needed, omit it for a single level. Needs --allow-write on the target path.

Remove files and directories

The second argument is recursive. Pass true to delete a directory tree, omit it for a single file. Needs --allow-write on the target path.

List a directory

Returns an array of names (files and directories), or undefined when the path is missing. Needs --allow-read on the directory.

Async variants

Same names with an Async suffix, each returning a Promise:
Failures reject instead of being swallowed (including writeFileAsync with data > 10 MB):
Permission denial is thrown synchronously, before the Promise is created:
Inside an async function that synchronous throw becomes a rejected promise. The flag and scope rules are identical to the sync calls. The second argument of readFileAsync is a mode: "buffer" / "binary" returns bytes; anything else returns a string.
Known issue: after a successful fs.*Async call the process may never exit — the promise settles and prints, then the loop waits on the fs pending counter forever. Failures exit normally. This is invisible inside servers; in short scripts prefer sync calls or process.exit().

Full example

Limits you will hit

  • Max path 4096 bytes.
  • Max read 10MB per call.
  • Max write 10MB per call — throws/rejects immediately above 10MB instead of truncating.
  • Sync I/O failures return undefined — no exception is raised, so a try/catch around a missing file catches nothing. Permission denial and write overflow are exceptions and do throw.
  • Scope roots are fixed at launch; process.chdir() does not move them.
  • No file watching — poll with timers instead.