Bun runtime

From aaione/everything-claude-code-zh · View interactive version

Bun runtime is an AI agent skill from the GitHub repository aaione/everything-claude-code-zh. Bun 作为运行时、包管理器、打包器和测试运行器。何时选择 Bun 而非 Node,迁移注意事项,以及 Vercel 支持。

How to use this skill

Skill file

Bun 运行时

Bun 是一个快速的一体化 JavaScript 运行时和工具链:运行时、包管理器、打包器和测试运行器。

何时使用

使用场景:采用 Bun、从 Node 迁移、编写或调试 Bun 脚本/测试,或在 Vercel 或其他平台上配置 Bun。

工作原理

从 Node 迁移:将 node script.js 替换为 bun run script.js 或 bun script.js。使用 bun install 替代 npm install;大多数包都能正常工作。使用 bun run 运行 npm 脚本;使用 bun x 进行 npx 风格的一次性运行。支持 Node 内置模块;在存在 Bun API 的地方优先使用 Bun API 以获得更好的性能。

Vercel:在项目设置中将运行时设为 Bun。构建:bun run build 或 bun build ./src/index.ts --outdir=dist。安装:使用 bun install --frozen-lockfile 进行可复现的部署。

示例

运行和安装

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

# 运行脚本或文件
bun run dev
bun run src/index.ts
bun src/index.ts

脚本和环境变量

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

测试

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

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

运行时 API

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

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

最佳实践

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.