Skip to main content
The 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


Reads the full contents of a file. By default returns raw bytes as a Uint8Array. Pass "utf8" as the second argument to decode the file and get a string instead.

fs.writeFile


Writes data to a file, replacing it if it already exists. Accepts a UTF-8 string, a Uint8Array, or an ArrayBuffer.

fs.appendFile


Appends 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.
Prefer the async variants (readFile, writeFile, appendFile) in event handlers and loops. Use the *Sync variants only during initialization where blocking I/O is acceptable.

Directories

fs.mkdir


Creates a directory. Pass true as the second argument to create the full path at once (like mkdir -p).

fs.rmdir


Removes a directory. Pass true to delete it recursively.

fs.readdir


Returns an array of entry names (files and subdirectories) inside path.

Metadata & checks

fs.exists


Returns true if the path exists (file or directory), false otherwise.

fs.stat


Returns a StatResult object describing the path.

Move, copy & watch


Deletes a single file. The async variant rejects if the file doesn’t exist; the sync variant returns false instead of throwing.

fs.rename


Moves or renames a file/directory within the sandbox.

fs.copyFile


Copies a file. Overwrites dst if it already exists.

fs.truncate


Resizes a file to length bytes. Defaults to 0 (clears the file). If the file is shorter than length, it is padded with null bytes.

fs.watch


Watches a file or directory for changes. Returns an 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 to scripts/data. Any path that escapes the sandbox throws "path escapes sandbox".
The following will always throw - absolute paths and directory traversal are blocked at the sandbox boundary: