選項

bun-runtime

affaan-m/ECC affaan-m/ECC

提供有關如何將 Bun 用作 JavaScript 執行環境、套件管理工具、打包工具及測試執行器的指引,內容涵蓋從 Node.js 遷移,以及 Vercel 部署。

...展開全部
0
更新時間 2026-10-01

Bun 執行環境

Bun 是一個快速且功能齊全的 JavaScript 執行環境與工具套件:集執行環境、套件管理器、打包工具及測試執行器於一身。

何時使用

  • 建議在以下情況使用 Bun:新的 JS/TS 專案、安裝/執行速度至關重要的腳本、使用 Bun 執行環境的 Vercel 部署,以及當您希望採用單一工具鏈(執行 + 安裝 + 測試 + 建置)時。
  • 建議在以下情況使用 Node:需要最大程度的生態系相容性、基於 Node 的舊版工具,或當某個依賴項存在已知的 Bun 問題時。

適用情境:採用 Bun、從 Node 遷移、編寫或除錯 Bun 腳本/測試,或在 Vercel 或其他平台上配置 Bun。

運作原理

  • 執行環境:可直接替換的 Node 相容執行環境(基於 JavaScriptCore 建構,以 Zig 實作)。
  • 套件管理器: bun install 速度遠快於 npm/yarn。鎖定檔在 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 及其生態系統發展迅速。
在 GitHub 上查看
---
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

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/ Claude 會自動偵測並使用該技能
儲存庫 affaan-m/ECC

相關技能

algorithmic-art
更新時間 2026-08-27
systematic-debugging
更新時間 2026-09-03
tech-debt-tracker
更新時間 2026-08-29
continual-learning
更新時間 2026-09-10
OR