Skip to main content
Bun provides fast file reading with Bun.file().

Read File to String

const file = Bun.file("data.txt");
const text = await file.text();
console.log(text);

Synchronous Read

import { readFileSync } from "node:fs";

const text = readFileSync("data.txt", "utf-8");
console.log(text);

Error Handling

try {
  const file = Bun.file("data.txt");
  const text = await file.text();
  console.log(text);
} catch (error) {
  console.error("Failed to read file:", error);
}
See File I/O for complete documentation.

Build docs developers (and LLMs) love