fs module gives each script a sandboxed file system rooted at its own
data/ folder - <your script>/data. Scripts cannot see each other’s files.
All paths are relative to your script’s
data/ folder. Absolute paths or
anything that escapes the sandbox (e.g. ../../secret) will throw
"path escapes sandbox". The folder is excluded from the file watcher, so
writing to it does not trigger a reload.Overview
Reading & writing
Read, write, and append file contents - binary by default, UTF-8 with an encoding option.
Directories
Create, remove, and list directory entries.
Metadata & checks
Check existence, read file size, and inspect types.
Move, copy & watch
Rename, copy, truncate, and watch paths for changes.
Reading & writing
fs.readFile
Uint8Array. Pass "utf8" as the second argument to decode the file
and get a string instead.
fs.writeFile
data to a file, replacing it if it already exists.
Accepts a UTF-8 string, a Uint8Array, or an ArrayBuffer.
fs.appendFile
data to the end of a file. Creates the file if it doesn’t exist.
Accepts a UTF-8 string, a Uint8Array, or an ArrayBuffer.
Directories
fs.mkdir
true as the second argument to create the full
path at once (like mkdir -p).
fs.rmdir
true to delete it recursively.
fs.readdir
path.
Metadata & checks
fs.exists
true if the path exists (file or directory), false otherwise.
fs.stat
StatResult object describing the path.
Move, copy & watch
fs.unlink
false instead of throwing.
fs.rename
fs.copyFile
dst if it already exists.
fs.truncate
length bytes. Defaults to 0 (clears the file).
If the file is shorter than length, it is padded with null bytes.
fs.watch
FSWatcher - call
.close() to stop.
Path utilities - fs.path
Pure string helpers - they do not touch the file system.
Recipes
JSON key-value store
Save slot manager
Log rotation
Error handling
All async functions return Promises that reject on failure. Sync functions throw directly.Security
All paths resolve relative toscripts/data. Any path that escapes the
sandbox throws "path escapes sandbox".