$npx -y skills add affaan-m/ECC --skill bun-runtimeBun as runtime, package manager, bundler, and test runner. When to choose Bun vs Node, migration notes, and Vercel support.
| 1 | # Bun Runtime |
| 2 | |
| 3 | Bun is a fast all-in-one JavaScript runtime and toolkit: runtime, package manager, bundler, and test runner. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - **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). |
| 8 | - **Prefer Node** for: maximum ecosystem compatibility, legacy tooling that assumes Node, or when a dependency has known Bun issues. |
| 9 | |
| 10 | Use when: adopting Bun, migrating from Node, writing or debugging Bun scripts/tests, or configuring Bun on Vercel or other platforms. |
| 11 | |
| 12 | ## How It Works |
| 13 | |
| 14 | - **Runtime**: Drop-in Node-compatible runtime (built on JavaScriptCore, implemented in Zig). |
| 15 | - **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). |
| 16 | - **Bundler**: Built-in bundler and transpiler for apps and libraries. |
| 17 | - **Test runner**: Built-in `bun test` with Jest-like API. |
| 18 | |
| 19 | **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. |
| 20 | |
| 21 | **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. |
| 22 | |
| 23 | ## Examples |
| 24 | |
| 25 | ### Run and install |
| 26 | |
| 27 | ```bash |
| 28 | # Install dependencies (creates/updates bun.lock or bun.lockb) |
| 29 | bun install |
| 30 | |
| 31 | # Run a script or file |
| 32 | bun run dev |
| 33 | bun run src/index.ts |
| 34 | bun src/index.ts |
| 35 | ``` |
| 36 | |
| 37 | ### Scripts and env |
| 38 | |
| 39 | ```bash |
| 40 | bun run --env-file=.env dev |
| 41 | FOO=bar bun run script.ts |
| 42 | ``` |
| 43 | |
| 44 | ### Testing |
| 45 | |
| 46 | ```bash |
| 47 | bun test |
| 48 | bun test --watch |
| 49 | ``` |
| 50 | |
| 51 | ```typescript |
| 52 | // test/example.test.ts |
| 53 | import { expect, test } from "bun:test"; |
| 54 | |
| 55 | test("add", () => { |
| 56 | expect(1 + 2).toBe(3); |
| 57 | }); |
| 58 | ``` |
| 59 | |
| 60 | ### Runtime API |
| 61 | |
| 62 | ```typescript |
| 63 | const file = Bun.file("package.json"); |
| 64 | const json = await file.json(); |
| 65 | |
| 66 | Bun.serve({ |
| 67 | port: 3000, |
| 68 | fetch(req) { |
| 69 | return new Response("Hello"); |
| 70 | }, |
| 71 | }); |
| 72 | ``` |
| 73 | |
| 74 | ## Best Practices |
| 75 | |
| 76 | - Commit the lockfile (`bun.lock` or `bun.lockb`) for reproducible installs. |
| 77 | - Prefer `bun run` for scripts. For TypeScript, Bun runs `.ts` natively. |
| 78 | - Keep dependencies up to date; Bun and the ecosystem evolve quickly. |