运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

读取 JSON 文件

Bun.file() 函数接受一个路径并返回一个 BunFile 实例。BunFile 扩展自 Blob,因此你可以使用多种格式以惰性方式读取文件。使用 .json() 可以读取并解析 .json 文件的内容,将其作为普通对象返回。

Bun 会相应地设置 BunFile 的 MIME 类型。

index.ts
const path = "/path/to/package.json";
const file = Bun.file(path);

const contents = await file.json();
// { name: "my-package" }

file.type; // => "application/json;charset=utf-8";