Bun runtime

From affaan-m/everything-claude-code · View interactive version

Bun runtime is an AI agent skill from the GitHub repository affaan-m/everything-claude-code. 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 add <pkg> / bun remove <pkg> / bun update [pkg] 管理依赖;bun outdated 列出过时的依赖。bun pm ls 使用锁文件列出项目依赖及解析后的版本,--all 包含传递依赖;它不检查已安装文件内容的完整性。CODE6 使用 bun.lock 中的包名和版本查询注册表漏洞公告,不修改 package.json、bun.lock 或 node_modules。发送私有包元数据前,请确认获批的发送目标。若作用域注册表没有漏洞公告端点,对应包会被跳过,且不影响退出码。bun audit fix 会升级并安装依赖,修改锁文件和已安装的包,有时也修改直接依赖的固定版本;请将其作为会产生修改的安装操作进行审查,包括适用的生命周期脚本。bun dedupe 消除重复安装,bun prune 移除不再被引用的包。多包仓库(monorepo)通过根 package.json 中的 workspaces 数组配置(与 npm/yarn 相同的约定);使用 bun run --filter <pkg-name> <script> 在指定工作区运行脚本。

内置 API:在添加依赖之前,优先考虑这些内置能力——文件 I/O 使用 Bun.file / Bun.write;内置 SQLite 数据库使用 bun:sqlite;HTTP/WebSocket 服务器使用 Bun.serve;SQL 数据库使用 CODE4;Redis 使用 CODE5;S3 兼容对象存储使用 CODE6。其余新特性参见 1.4 发布说明。

Vercel:在 vercel.json 中设置 bunVersion: "1.4.x" 以使用 Bun 1.4(Rust 运行时)。详见 Vercel 的 Bun 运行时文档。构建命令:bun run build 或 bun build ./src/index.ts --outdir=dist。安装命令:bun install --frozen-lockfile 用于可重复的部署。

参考:Bun 1.4 发布说明。

示例

运行和安装

# Install dependencies (creates/updates bun.lock)
bun install

# Run a script or file
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

Bun 1.4 中可用于 CI 的选项包括:选择受影响测试的 bun test --changed[=<ref>]、提供逐文件全局环境的 bun test --isolate、使用工作进程的 bun test --parallel[=<n>],以及划分 CI 任务的 bun test --shard=<n>/<count>。--isolate 在同一进程内为每个文件创建新的 JavaScript 全局环境,并清理文档列出的逐文件资源;它不是进程或安全沙箱。--parallel 默认启用该选项。--timings=<file> 读取以往耗时以供调度,--update-timings 记录耗时。示例:bun test --parallel --timings=./test-timings.json --update-timings。为多个分片选择耗时文件路径前,请查阅已安装版本的帮助和文档。

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

test("add", () => {
  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.