| name | Bun |
| description | Use when building, testing, or deploying JavaScript/TypeScript applications. Reach for Bun when you need to run scripts, install dependencies, bundle code, or test applications with a single unified toolkit that replaces Node.js, npm, and other build tools. |
| metadata | {"mintlify-proj":"bun","version":"1.0"} |
Bun Skill Reference
Product Summary
Bun is an all-in-one JavaScript/TypeScript toolkit that replaces Node.js, npm, and build tools. It includes a fast runtime (powered by JavaScriptCore), package manager, bundler, and test runner—all in a single executable. Key files: bunfig.toml (configuration), package.json (dependencies and scripts), bun.lock (lockfile). Primary commands: bun run, bun install, bun build, bun test. See https://bun.com/docs for complete documentation.
When to Use
- Running code: Execute
.js, .ts, .jsx, .tsx files directly without compilation steps
- Package management: Install, add, remove, or update npm packages 25x faster than npm
- Building: Bundle TypeScript, JSX, React, and CSS for browsers or servers
- Testing: Run Jest-compatible tests with TypeScript support and watch mode
- Scripts: Execute
package.json scripts 28x faster than npm
- Monorepos: Manage workspaces with isolated or hoisted dependency strategies
- Migrating from Node.js: Drop-in replacement for existing Node.js projects with minimal changes
Quick Reference
Core Commands
| Task | Command | Notes |
|---|
| Run a file | bun run index.ts | Supports .ts, .tsx, .jsx natively |
| Run a script | bun run dev | From package.json scripts |
| Install deps | bun install | Creates bun.lock lockfile |
| Add package | bun add react | Adds to package.json and installs |
| Add dev dep | bun add -d @types/node | Adds to devDependencies |
| Remove package | bun remove react | Removes from package.json |
| Bundle code | bun build ./index.ts --outdir ./dist | Outputs to directory |
| Run tests | bun test | Finds *.test.ts, *.spec.ts files |
| Watch mode | bun --watch run index.ts | Re-runs on file changes |
| Execute package | bunx cowsay hello | Like npx |
Configuration File: bunfig.toml
Place in project root alongside package.json. Optional—Bun works without it.
[install]
optional = true
dev = true
peer = true
production = false
linker = "hoisted"
[test]
root = "."
coverage = false
timeout = 5000
randomize = false
[serve]
port = 3000
[run]
shell = "system"
bun = true
silent = false
[define]
"process.env.API_URL" = "'https://api.example.com'"
File Type Support
Bun transpiles on the fly. No configuration needed.
| Extension | Handled As | Notes |
|---|
.js, .mjs | JavaScript | ESM or CommonJS |
.ts, .mts | TypeScript | Transpiled to JS |
.jsx | JSX | Transpiled with React factory |
.tsx | TypeScript + JSX | Full support |
.json, .jsonc | JSON | Imported as objects |
.toml, .yaml | Config files | Imported as objects |
.html | HTML | Bundler processes assets |
.css | Stylesheets | Bundled together |
Package Manager Flags
bun install --production
bun install --frozen-lockfile
bun install --dry-run
bun install --linker isolated
bun install --linker hoisted
bun install --global
bun install --verbose
Test Runner Flags
bun test --watch
bun test --concurrent
bun test --timeout 10000
bun test --bail
bun test --retry 3
bun test --coverage
bun test -t "pattern"
Bundler Options
bun build ./index.ts --outdir ./dist
bun build ./index.ts --outdir ./dist --minify
bun build ./index.ts --outdir ./dist --sourcemap linked
bun build ./index.ts --outdir ./dist --target browser
bun build ./index.ts --outdir ./dist --format esm
bun build ./index.ts --outdir ./dist --splitting
bun build ./index.ts --outdir ./dist --watch
Decision Guidance
When to Use Hoisted vs. Isolated Installs
| Scenario | Use | Why |
|---|
| New monorepo/workspace | isolated | Prevents phantom dependencies |
| New single-package project | hoisted | Traditional npm behavior |
| Existing project (pre-v1.3.2) | hoisted | Backward compatibility |
| Strict dependency enforcement | isolated | Packages only access declared deps |
| Maximum compatibility | hoisted | Works like npm/yarn |
When to Use bun build vs. bun run
| Use Case | Tool | Why |
|---|
| Development, testing | bun run | Fast startup, no bundling overhead |
| Production deployment | bun build | Optimized, minified, single file |
| Browser apps | bun build --target browser | Outputs ES modules for <script type="module"> |
| Server apps | bun build --target bun | Optimized for Bun runtime |
| Node.js compatibility | bun build --target node | Outputs CommonJS |
When to Use bun test vs. External Test Runners
| Scenario | Use Bun | Use External |
|---|
| TypeScript tests | ✓ | Only if Jest/Vitest already configured |
| Jest compatibility needed | ✓ | If advanced features required |
| DOM/UI testing | ✓ | With HappyDOM or Testing Library |
| Snapshot testing | ✓ | If Jest snapshots needed |
| Custom reporters | ✗ | Use Jest/Vitest |
Workflow
1. Initialize a New Project
bun init my-app
cd my-app
Choose template: Blank, React, or Library. Creates package.json, tsconfig.json, bunfig.toml, and starter files.
2. Install Dependencies
bun install
bun add react
bun add -d @types/node
bun install --frozen-lockfile
Check bun.lock into version control for reproducible installs.
3. Run Code or Scripts
bun run index.ts
bun run dev
bun --watch run index.ts
Bun transpiles TypeScript/JSX on the fly. No build step needed for development.
4. Write and Run Tests
import { test, expect } from "bun:test";
test("2 + 2 = 4", () => {
expect(2 + 2).toBe(4);
});
bun test
bun test --watch
bun test --coverage
Tests auto-discover files matching *.test.ts, *.spec.ts, etc.
5. Build for Production
bun build ./src/index.tsx --outdir ./dist --minify
bun build ./src/server.ts --outdir ./dist --target bun --minify
bun build ./cli.ts --outfile mycli --compile
Output is optimized, minified, and ready to deploy.
6. Configure bunfig.toml (Optional)
[install]
linker = "isolated"
[test]
coverage = true
timeout = 10000
[run]
bun = true
Override defaults as needed. Most projects work without this file.
Common Gotchas
-
Lifecycle scripts disabled by default: Bun doesn't run postinstall scripts for security. Add trusted packages to trustedDependencies in package.json to allow them.
-
Flags go after bun, not after the command: Use bun --watch run dev, not bun run dev --watch. Flags after the command are passed to the script itself.
-
bun.lock is binary by default: Prior to Bun 1.2, lockfiles were binary (bun.lockb). Upgrade with bun install --save-text-lockfile --frozen-lockfile --lockfile-only, then delete bun.lockb.
-
Node.js shebang scripts run with Node by default: Scripts with #!/usr/bin/env node run with Node.js. Use bun run --bun script to force Bun instead.
-
Environment variables not auto-loaded in bunfig.toml by default: Use $VARIABLE syntax in bunfig.toml to reference env vars. Bun loads .env, .env.local, .env.[NODE_ENV] automatically at runtime.
-
Peer dependencies installed by default: Unlike npm, Bun installs peerDependencies automatically. Disable with peer = false in bunfig.toml or --omit peer flag.
-
Auto-install can mask missing dependencies: With install.auto = "auto" (default), missing packages are installed on the fly. In CI, use --frozen-lockfile to catch missing deps.
-
Isolated linker requires explicit hoisting for some packages: With linker = "isolated", packages can only access declared dependencies. Use publicHoistPattern to hoist specific packages to root node_modules.
-
TypeScript errors don't block execution: Bun runs .ts files even with type errors. Use a separate type-checker (tsc --noEmit) in CI if strict type checking is required.
-
CommonJS require() works but ESM is preferred: Bun supports both, but ESM is the default. Use import statements for better tree-shaking and bundler optimization.
Verification Checklist
Before submitting work with Bun:
Resources
For additional documentation and navigation, see: https://bun.com/docs/llms.txt