Skip to main content
The Bun.file and Bun.write APIs documented on this page are heavily optimized and are the recommended way to work with files in Bun. For operations they don’t cover, such as mkdir or readdir, use Bun’s nearly complete implementation of the node:fs module.

Reading files (Bun.file())

Bun.file(path): BunFile Create a BunFile instance with the Bun.file(path) function. A BunFile represents a lazily-loaded file; initializing it does not read the file from disk.
The reference conforms to the Blob interface, so the contents can be read in various formats.
You can also create file references from numerical file descriptors or file:// URLs.
A BunFile can point to a location on disk where a file does not exist.
The default MIME type is text/plain;charset=utf-8. Override it by passing a second argument to Bun.file.
For convenience, Bun exposes stdin, stdout and stderr as instances of BunFile.

Deleting files (file.delete())

Call .delete() to delete a file.

Writing files (Bun.write())

Bun.write(destination, data): Promise<number> The Bun.write function is a multi-tool for writing payloads of all kinds to disk. The first argument is the destination, which can be any of the following:
  • string: A path to a location on the file system. Use the "path" module to manipulate paths.
  • URL: A file:// descriptor.
  • BunFile: A file reference.
The second argument is the data to write. It can be any of the following:
  • string
  • Blob (including BunFile)
  • ArrayBuffer or SharedArrayBuffer
  • TypedArray (Uint8Array, et. al.)
  • Response
Bun handles each combination with the fastest available system call on the current platform.
To write a string to disk:
To copy a file to another location on disk:
To write a byte array to disk:
To write a file to stdout:
To write the body of an HTTP response to disk:

Incremental writing with FileSink

Bun provides a native incremental file writing API called FileSink. To retrieve a FileSink instance from a BunFile:
To incrementally write to the file, call .write().
These chunks are buffered internally. To flush the buffer to disk, use .flush(). This returns the number of flushed bytes.
The buffer also auto-flushes when the FileSink’s high water mark is reached; that is, when its internal buffer is full. You can configure this value.
To flush the buffer and close the file:
By default, the bun process stays alive until this FileSink is explicitly closed with .end(). To opt out of this behavior, “unref” the instance.

Directories

Bun’s implementation of node:fs is fast. Use node:fs for working with directories in Bun.

Reading directories (readdir)

To read a directory in Bun, use readdir from node:fs.

Reading directories recursively

To recursively read a directory in Bun, use readdir with recursive: true.

Creating directories (mkdir)

To recursively create a directory, use mkdir in node:fs:

Benchmarks

The following is a 3-line implementation of the Linux cat command.
cat.ts
To run the file:
terminal
It runs 2x faster than GNU cat for large files on Linux.
Cat screenshot

Reference