bun-runtime
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBun 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: is significantly faster than npm/yarn. Lockfile is
bun install(text) by default in current Bun; older versions usedbun.lock(binary).bun.lockb - Bundler: Built-in bundler and transpiler for apps and libraries.
- Test runner: Built-in with Jest-like API.
bun test
Migration from Node: Replace with or . Run in place of ; most packages work. Use for npm scripts; for npx-style one-off runs. Node built-ins are supported; prefer Bun APIs where they exist for better performance.
node script.jsbun run script.jsbun script.jsbun installnpm installbun runbun xVercel: Set runtime to Bun in project settings. Build: or . Install: for reproducible deploys.
bun run buildbun build ./src/index.ts --outdir=distbun install --frozen-lockfile- 运行时:可直接替代Node的兼容运行时(基于JavaScriptCore构建,使用Zig语言实现)。
- 包管理器:比npm/yarn快得多。当前版本的Bun默认使用文本格式的
bun install作为锁文件;旧版本使用二进制格式的bun.lock。bun.lockb - 打包工具:内置用于应用和库的打包器和转译器。
- 测试运行器:内置,拥有类Jest的API。
bun test
从Node迁移:将替换为或。使用替代;大多数包都能正常工作。使用执行npm脚本;使用实现类似npx的一次性运行。Node内置模块均受支持;优先使用Bun提供的API以获得更好的性能。
node script.jsbun run script.jsbun script.jsbun installnpm installbun runbun xVercel支持:在项目设置中将运行时设置为Bun。构建命令:或。安装命令:以实现可复现的部署。
bun run buildbun build ./src/index.ts --outdir=distbun install --frozen-lockfileExamples
示例
Run and install
运行与安装
bash
undefinedbash
undefinedInstall 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
undefinedbun run dev
bun run src/index.ts
bun src/index.ts
undefinedScripts and env
脚本与环境变量
bash
bun run --env-file=.env dev
FOO=bar bun run script.tsbash
bun run --env-file=.env dev
FOO=bar bun run script.tsTesting
测试
bash
bun test
bun test --watchtypescript
// test/example.test.ts
import { expect, test } from "bun:test";
test("add", () => {
expect(1 + 2).toBe(3);
});bash
bun test
bun test --watchtypescript
// 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 (or
bun.lock) for reproducible installs.bun.lockb - Prefer for scripts. For TypeScript, Bun runs
bun runnatively..ts - Keep dependencies up to date; Bun and the ecosystem evolve quickly.
- 提交锁文件(或
bun.lock)以确保可复现的安装。bun.lockb - 优先使用执行脚本。对于TypeScript,Bun可直接运行
bun run文件。.ts - 保持依赖项更新;Bun及其生态系统发展迅速。