bun-runtime

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Bun Runtime

Bun运行时

Bun is a fast all-in-one JavaScript runtime and toolkit: runtime, package manager, bundler, and test runner.
Bun是一款快速的一体化JavaScript运行时和工具集:包含运行时、包管理器、打包工具和测试运行器。

When to Use

适用场景

  • Prefer Bun for: new JS/TS projects, scripts where install/run speed matters, Vercel deployments with Bun runtime, and when you want a single toolchain (run + install + test + build).
  • Prefer Node for: maximum ecosystem compatibility, legacy tooling that assumes Node, or when a dependency has known Bun issues.
Use when: adopting Bun, migrating from Node, writing or debugging Bun scripts/tests, or configuring Bun on Vercel or other platforms.
  • 优先选择Bun的场景:新的JS/TS项目、对安装/运行速度有要求的脚本、使用Bun运行时的Vercel部署,以及希望使用单一工具链(运行+安装+测试+构建)的情况。
  • 优先选择Node的场景:需要最大程度的生态兼容性、依赖于Node的旧有工具,或者某个依赖项存在已知的Bun兼容问题时。
适用于:采用Bun、从Node迁移、编写或调试Bun脚本/测试,或者在Vercel或其他平台上配置Bun的场景。

How It Works

工作原理

  • Runtime: Drop-in Node-compatible runtime (built on JavaScriptCore, implemented in Zig).
  • Package manager:
    bun install
    is significantly faster than npm/yarn. Lockfile is
    bun.lock
    (text) by default in current Bun; older versions used
    bun.lockb
    (binary).
  • Bundler: Built-in bundler and transpiler for apps and libraries.
  • Test runner: Built-in
    bun test
    with Jest-like API.
Migration from Node: Replace
node script.js
with
bun run script.js
or
bun script.js
. Run
bun install
in place of
npm install
; most packages work. Use
bun run
for npm scripts;
bun x
for npx-style one-off runs. Node built-ins are supported; prefer Bun APIs where they exist for better performance.
Vercel: Set runtime to Bun in project settings. Build:
bun run build
or
bun build ./src/index.ts --outdir=dist
. Install:
bun install --frozen-lockfile
for reproducible deploys.
  • 运行时:可直接替代Node的兼容运行时(基于JavaScriptCore构建,使用Zig语言实现)。
  • 包管理器
    bun install
    比npm/yarn快得多。当前版本的Bun默认使用文本格式的
    bun.lock
    作为锁文件;旧版本使用二进制格式的
    bun.lockb
  • 打包工具:内置用于应用和库的打包器和转译器。
  • 测试运行器:内置
    bun test
    ,拥有类Jest的API。
从Node迁移:将
node script.js
替换为
bun run script.js
bun script.js
。使用
bun install
替代
npm install
;大多数包都能正常工作。使用
bun run
执行npm脚本;使用
bun x
实现类似npx的一次性运行。Node内置模块均受支持;优先使用Bun提供的API以获得更好的性能。
Vercel支持:在项目设置中将运行时设置为Bun。构建命令:
bun run build
bun build ./src/index.ts --outdir=dist
。安装命令:
bun install --frozen-lockfile
以实现可复现的部署。

Examples

示例

Run and install

运行与安装

bash
undefined
bash
undefined

Install dependencies (creates/updates bun.lock or bun.lockb)

安装依赖(创建/更新bun.lock或bun.lockb)

bun install
bun install

Run a script or file

运行脚本或文件

bun run dev bun run src/index.ts bun src/index.ts
undefined
bun run dev bun run src/index.ts bun src/index.ts
undefined

Scripts and env

脚本与环境变量

bash
bun run --env-file=.env dev
FOO=bar bun run script.ts
bash
bun run --env-file=.env dev
FOO=bar bun run script.ts

Testing

测试

bash
bun test
bun test --watch
typescript
// test/example.test.ts
import { expect, test } from "bun:test";

test("add", () => {
  expect(1 + 2).toBe(3);
});
bash
bun test
bun test --watch
typescript
// test/example.test.ts
import { expect, test } from "bun:test";

test("加法测试", () => {
  expect(1 + 2).toBe(3);
});

Runtime API

运行时API

typescript
const file = Bun.file("package.json");
const json = await file.json();

Bun.serve({
  port: 3000,
  fetch(req) {
    return new Response("Hello");
  },
});
typescript
const file = Bun.file("package.json");
const json = await file.json();

Bun.serve({
  port: 3000,
  fetch(req) {
    return new Response("Hello");
  },
});

Best Practices

最佳实践

  • Commit the lockfile (
    bun.lock
    or
    bun.lockb
    ) for reproducible installs.
  • Prefer
    bun run
    for scripts. For TypeScript, Bun runs
    .ts
    natively.
  • Keep dependencies up to date; Bun and the ecosystem evolve quickly.
  • 提交锁文件(
    bun.lock
    bun.lockb
    )以确保可复现的安装。
  • 优先使用
    bun run
    执行脚本。对于TypeScript,Bun可直接运行
    .ts
    文件。
  • 保持依赖项更新;Bun及其生态系统发展迅速。