Bun.file and Bun.write are heavily optimized and are the recommended way to perform file-system tasks in Bun. For operations not yet available natively—such as mkdir or readdir—use Bun’s nearly complete implementation of node:fs.Reading files with Bun.file()
Bun.file(path) returns a BunFile instance — a lazily-loaded reference to a file on disk. The file is not read until you call a method on it.
BunFile conforms to the Blob interface. Read its contents in multiple formats:
File references
You can create aBunFile using a path string, a file descriptor number, or a file:// URL:
File metadata
BunFile can reference a path that doesn’t exist yet. No error is thrown until you try to read it:
Standard I/O streams
Bun exposesstdin, stdout, and stderr as BunFile instances:
Deleting files
Writing files with Bun.write()
Bun.write(destination, data) writes data to disk and returns a Promise<number> (bytes written).
Destination can be a string path, file:// URL, or BunFile. Data can be a string, Blob, BunFile, ArrayBuffer, TypedArray, or Response.
copy_file_range on Linux, clonefile on macOS).
Incremental writing with FileSink
For streaming or incremental writes, use FileSink — Bun’s native incremental file writer.
Basic usage
High water mark
Control when the buffer auto-flushes by setting ahighWaterMark:
Process lifetime
By default, the Bun process stays alive untilFileSink is closed. Opt out with .unref():
Module path helpers
Bun provides module-relative path helpers onimport.meta:
Directory operations
Bun does not yet have a native directory API. Usenode:fs instead.
API reference
Glob
Bun.Glob provides fast native file globbing. Use it to scan directories or match strings against patterns.
*, **, ?, [abc], {a,b}, and negation with !.