title: Filesystem description: File access — sync calls and *Async Promise variants: read, write, exists, mkdir, rm, readdir. order: 4
Filesystem
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.
Read a file
Returns the file content as a string, or undefined when the read fails
(missing file, permission denied) — no exception is thrown. Reads cap at
10MB.
Write a file
Creates the file when missing, overwrites when present. Parent directories must exist — use mkdir first.
Check existence
Returns true or false. Never throws.
Make directories
The second argument is recursive. Pass true to create parents as needed, omit it for a single level.
Remove files and directories
The second argument is recursive. Pass true to delete a directory tree, omit it for a single file.
List a directory
Returns an array of names (files and directories), or undefined when
the path is missing.
Async variants
Same names with an Async suffix, each returning a Promise:
Failures reject instead of being swallowed:
The second argument of readFileAsync is a mode: "buffer" / "binary"
returns bytes; anything else returns a string.
Known issue: after a successful
fs.*Asynccall 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 orprocess.exit().
Full example
Limits you will hit
- Max path 4096 bytes.
- Max read 10MB per call.
- Sync failures return
undefined— no exception is raised, sotry/catcharound sync calls catches nothing. - No file watching — poll with timers instead.
