Skip to main content
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 a BunFile using a path string, a file descriptor number, or a file:// URL:

File metadata

A BunFile can reference a path that doesn’t exist yet. No error is thrown until you try to read it:
To override the inferred MIME type, pass a second argument:

Standard I/O streams

Bun exposes stdin, 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.
Bun automatically selects the fastest available system call for each input/output combination (e.g., 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 a highWaterMark:

Process lifetime

By default, the Bun process stays alive until FileSink is closed. Opt out with .unref():

Module path helpers

Bun provides module-relative path helpers on import.meta:
These are useful for constructing paths relative to the current source file:

Directory operations

Bun does not yet have a native directory API. Use node:fs instead.

API reference

Glob

Bun.Glob provides fast native file globbing. Use it to scan directories or match strings against patterns.
Match a string against a pattern:
Scan options:
Synchronous scan:
Supported glob syntax: *, **, ?, [abc], {a,b}, and negation with !.