bun-runtime
affaan-m/ECC
提供了关于如何将 Bun 用作 JavaScript 运行时、包管理器、打包工具和测试运行器的指导,包括从 Node.js 迁移以及 Vercel 部署的相关内容。
...展开全部Bun 运行时
Bun 是一个快速的一体化 JavaScript 运行时和工具包:集运行时、包管理器、打包工具和测试运行器于一体。
何时使用
- 建议在以下场景使用 Bun:新的 JS/TS 项目、对安装/运行速度有要求的脚本、使用 Bun 运行时的 Vercel 部署,以及需要单一工具链(运行 + 安装 + 测试 + 构建)的情况。
- 建议在以下情况下使用 Node:需要最大程度的生态系统兼容性、依赖于 Node 的传统工具,或者当某个依赖项存在已知的 Bun 兼容性问题时。
适用场景:采用 Bun、从 Node 迁移、编写或调试 Bun 脚本/测试,或在 Vercel 及其他平台上配置 Bun。
工作原理
- 运行时:即插即用的 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;大多数包都能正常工作。对于 npm 脚本,请使用 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 用于可重现的部署。
示例
运行并安装
# Install dependencies (creates/updates bun.lock or bun.lockb)
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
// 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");
},
});
最佳实践
- 提交锁定文件(
bun.lock或bun.lockb)以实现可重现的安装。 - 建议
bun run脚本。对于 TypeScript,Bun 会.ts原生运行。 - 请保持依赖项最新;Bun及其生态系统发展迅速。
---
name: bun-runtime
description: Provides guidance on using Bun as a JavaScript runtime, package manager, bundler, and test runner, including migration from Node.js and Vercel deployment.
---
# Bun Runtime
Bun is a fast all-in-one JavaScript runtime and toolkit: runtime, package manager, bundler, and test runner.
## 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.
## 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.
## Examples
### Run and install
```bash
# Install dependencies (creates/updates bun.lock or bun.lockb)
bun install
# Run a script or file
bun run dev
bun run src/index.ts
bun src/index.ts
```
### Scripts and env
```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);
});
```
### Runtime API
```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.
所有文件
1 个文件安装 bun-runtime
下载技能文件并将其解压到 .claude/skills/ 目录下。
下载ZIP克隆仓库并复制技能文件到您的项目中。
git clone https://github.com/affaan-m/ECC/tree/main/skills/bun-runtime # Copy SKILL.md to your .claude/skills/ directory
复制





首页
