Use this file to discover all available pages before exploring further.
The Bun.file and Bun.write APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. For operations that are not yet available with Bun.file, such as mkdir or readdir, you can use Bun’s nearly complete implementation of the node:fs module.
Bun.file(path): BunFileCreate a BunFile instance with the Bun.file(path) function. A BunFile represents a lazily-loaded file; initializing it does not actually read the file from disk.
const foo = Bun.file("foo.txt"); // relative to cwdfoo.size; // number of bytesfoo.type; // MIME type
The reference conforms to the Blob interface, so the contents can be read in various formats.
const foo = Bun.file("foo.txt");await foo.text(); // contents as a stringawait foo.json(); // contents as a JSON objectawait foo.stream(); // contents as ReadableStreamawait foo.arrayBuffer(); // contents as ArrayBufferawait foo.bytes(); // contents as Uint8Array
File references can also be created using numerical file descriptors or file:// URLs.
Bun.file(1234);Bun.file(new URL(import.meta.url)); // reference to the current file
A BunFile can point to a location on disk where a file does not exist.
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 have any of the following types:
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 be written. It can be any of the following:
string
Blob (including BunFile)
ArrayBuffer or SharedArrayBuffer
TypedArray (Uint8Array, et. al.)
Response
All possible permutations are handled using the fastest available system calls on the current platform.
See syscalls
Output
Input
System call
Platform
file
file
copy_file_range
Linux
file
pipe
sendfile
Linux
pipe
pipe
splice
Linux
terminal
file
sendfile
Linux
terminal
terminal
sendfile
Linux
socket
file or pipe
sendfile (if http, not https)
Linux
file (doesn’t exist)
file (path)
clonefile
macOS
file (exists)
file
fcopyfile
macOS
file
Blob or string
write
macOS
file
Blob or string
write
Linux
To write a string to disk:
const data = `It was the best of times, it was the worst of times.`;await Bun.write("output.txt", data);
To incrementally write to the file, call .write().
const file = Bun.file("output.txt");const writer = file.writer();writer.write("it was the best of times\n");writer.write("it was the worst of times\n");
These chunks will be buffered internally. To flush the buffer to disk, use .flush(). This returns the number of flushed bytes.
writer.flush(); // write buffer to disk
The buffer will also auto-flush when the FileSink’s high water mark is reached; that is, when its internal buffer is full. This value can be configured.
Note that, by default, the bun process will stay alive until this FileSink is explicitly closed with .end(). To opt out of this behavior, you can “unref” the instance.
writer.unref();// to "re-ref" it laterwriter.ref();
To recursively read a directory in Bun, use readdir with recursive: true.
import { readdir } from "node:fs/promises";// read all the files in the current directory, recursivelyconst files = await readdir("../", { recursive: true });