| name | Bun |
| description | Use when building JavaScript/TypeScript applications, setting up HTTP servers, managing dependencies, bundling code, running tests, or working with full-stack applications. Bun is a complete JavaScript runtime, package manager, bundler, and test runner that replaces Node.js, npm, and other tools. |
| metadata | {"mintlify-proj":"bun","version":"1.0"} |
Bun Skill
Product summary
Bun is a complete JavaScript runtime, package manager, bundler, and test runner written in Zig. It replaces Node.js, npm, yarn, esbuild, and Jest with a single fast binary. Use Bun to run TypeScript/JSX directly, install dependencies, bundle applications, and run tests. Key files: bunfig.toml (configuration), package.json (dependencies), bun.lock (lockfile). Primary CLI commands: bun run, bun install, bun build, bun test. See https://bun.com/docs for complete documentation.
When to use
Reach for this skill when:
- Running code: Execute JavaScript, TypeScript, or JSX files directly without compilation steps
- Managing dependencies: Install, add, remove, or update npm packages faster than npm/yarn/pnpm
- Building applications: Bundle JavaScript/TypeScript for browsers, Node.js, or Bun runtime
- Testing: Write and run Jest-compatible tests with built-in test runner
- Starting servers: Create HTTP servers with
Bun.serve() for APIs, full-stack apps, or WebSockets
- File I/O: Read/write files, streams, and binary data with optimized APIs
- Monorepos: Manage workspaces with isolated or hoisted dependency linking
- Full-stack development: Bundle HTML imports to serve frontend and backend from single executable
Quick reference
Essential commands
| Task | Command |
|---|
| Run a file | bun run index.ts or bun index.ts |
| Run a script | bun run dev (from package.json) |
| Install dependencies | bun install |
| Add a package | bun add react or bun add -d @types/node |
| Remove a package | bun remove react |
| Run tests | bun test |
| Build/bundle | bun build ./index.ts --outdir ./dist |
| Watch mode | bun --watch run index.ts or bun build --watch |
| Create project | bun init my-app |
Configuration file: bunfig.toml
Located at project root or ~/.bunfig.toml. Optional but useful for:
[install]
linker = "hoisted"
optional = true
dev = true
peer = true
[test]
coverage = false
timeout = 5000
[run]
shell = "system"
bun = true
[serve]
port = 3000
File type support
Bun transpiles on-the-fly:
.js, .jsx, .ts, .tsx — JavaScript/TypeScript with JSX
.json, .jsonc, .toml, .yaml — Parsed at build time
.html — Full-stack bundling with asset processing
.css — Bundled into single file
.wasm, .node — Supported as assets
Common patterns
HTTP server:
Bun.serve({
port: 3000,
routes: {
'/': () => new Response('Hello'),
'/api/users/:id': (req) => new Response(`User ${req.params.id}`),
},
});
Read/write files:
const file = Bun.file('path.txt');
const text = await file.text();
await Bun.write('output.txt', 'content');
Test:
import { test, expect } from 'bun:test';
test('math', () => expect(2 + 2).toBe(4));
Bundle:
await Bun.build({
entrypoints: ['./index.tsx'],
outdir: './dist',
minify: true,
});
Decision guidance
| Scenario | Use | Why |
|---|
| Dependency linking | linker = "hoisted" | Traditional npm behavior, simpler for single packages |
| Dependency linking | linker = "isolated" | Prevents phantom dependencies, required for monorepos |
| Package manager | bun install | 25x faster than npm, compatible with existing projects |
| Bundler target | target: "browser" | Client-side code, prioritizes browser exports |
| Bundler target | target: "bun" | Server code, optimized for Bun runtime |
| Bundler target | target: "node" | Node.js compatibility, outputs .mjs |
| Test execution | --concurrent | Independent async tests, faster suites |
| Test execution | test.serial | Tests with shared state or order dependencies |
| HTTP handler | routes object | Simple routing, static/dynamic routes |
| HTTP handler | fetch function | Complex logic, middleware, custom routing |
| File I/O | Bun.file() + Bun.write() | Optimized for Bun, recommended approach |
| File I/O | node:fs module | Operations not yet in Bun API (mkdir, readdir) |
Workflow
Running a TypeScript project
- Check project structure: Verify
package.json exists and lists dependencies
- Install dependencies: Run
bun install (creates bun.lock)
- Run code: Execute
bun run index.ts or bun run <script> from package.json
- Watch for changes: Add
--watch flag for development: bun --watch run dev
- Verify output: Check console for errors or expected behavior
Building for production
- Review source: Identify entry points and dependencies
- Configure build: Create
bunfig.toml or pass CLI flags for minification, target, format
- Run build:
bun build ./src/index.ts --outdir ./dist --minify
- Check output: Verify bundled files in
./dist directory
- Test bundle: Run the bundled code to confirm it works
- Deploy: Upload dist folder or compiled executable to production
Setting up HTTP server
- Create handler: Write
Bun.serve() with fetch or routes
- Configure port: Set
port in options or via BUN_PORT env var
- Add routes: Define static/dynamic routes or use
fetch for custom logic
- Test locally: Run
bun run server.ts and visit http://localhost:3000
- Add middleware: Wrap routes with auth, logging, or error handling as needed
- Deploy: Use
bun build --compile for standalone executable or deploy source
Writing and running tests
- Create test file: Name it
*.test.ts or *.spec.ts
- Import test utilities:
import { test, expect, describe } from "bun:test"
- Write tests: Use
test() for individual tests, describe() for grouping
- Run tests: Execute
bun test to discover and run all test files
- Filter tests: Use
bun test --test-name-pattern add to run specific tests
- Watch mode: Add
--watch for re-running on file changes
- Check coverage: Run
bun test --coverage to see coverage reports
Common gotchas
- Shebang in scripts: Bun respects
#!/usr/bin/env node shebangs; use bun run --bun to force Bun execution instead
- Auto-install disabled in production: Set
install.auto = "disable" in bunfig.toml for CI/CD to prevent runtime package resolution
- Lockfile format: Bun uses
bun.lock (text) by default since v1.2; old bun.lockb (binary) must be migrated
- TypeScript errors in Bun global: Install
@types/bun and add "lib": ["ESNext"] to tsconfig.json
- Lifecycle scripts security: Bun doesn't run postinstall scripts by default; add packages to
trustedDependencies in package.json to allow them
- Peer dependencies: Bun installs peer dependencies by default (unlike npm); use
--omit peer to skip them
- Watch mode flag placement: Put Bun flags immediately after
bun: bun --watch run dev ✓, not bun run dev --watch ✗
- Bundler not for type-checking: Use
tsc separately for type checking; Bun's bundler transpiles but doesn't validate types
- Minification by default for Bun target: When
target: "bun", identifiers are minified automatically; disable with minify: false
- External imports in bundles: Mark packages as external to exclude them:
external: ["lodash"] leaves import statement in bundle
- Idle timeout on streams:
Bun.serve closes idle connections after 10 seconds; disable per-request with server.timeout(req, 0) for SSE/long-polling
Verification checklist
Before submitting work with Bun:
Resources
For additional documentation and navigation, see: https://bun.com/docs/llms.txt