Bun
Bun Skill Reference
Product Summary
Bun is an all-in-one JavaScript/TypeScript toolkit that replaces Node.js, npm, and bundlers with a single fast binary. It includes a runtime (powered by JavaScriptCore), package manager, test runner, and bundler. Key files: bunfig.toml (configuration), package.json (scripts and dependencies), bun.lock (lockfile). Primary CLI commands: bun run, bun install, bun test, bun build. See https://bun.com/docs for comprehensive documentation.
When to Use
- Running scripts: Execute
.js,.ts,.jsx,.tsxfiles directly withbun runorbun <file>— no compilation step needed - Package management: Install dependencies with
bun install(25x faster than npm) or add packages withbun add - Testing: Write and run Jest-compatible tests with
bun testwith TypeScript support built-in - Bundling: Bundle applications for browsers or servers with
bun buildorBun.build()API - HTTP servers: Build servers with
Bun.serve()API with native WebSocket and streaming support - Monorepos: Manage workspaces with
bun install --filterand run scripts across packages - Development: Use watch mode (
--watch) for live reloading during development - Deployment: Compile standalone executables with
bun build --compileor deploy to Vercel, Railway, etc.
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 |
| Watch tests | bun test --watch |
| Build for browser | bun build ./index.tsx --outdir ./dist |
| Build for server | bun build ./index.tsx --outdir ./dist --target bun |
| Watch build | bun build ./index.tsx --outdir ./dist --watch |
| Run with watch mode | bun --watch run index.ts |
| Execute a package | bunx cowsay "Hello" |
Configuration Files
| File | Purpose |
|---|---|
bunfig.toml |
Bun-specific configuration (optional, zero-config by default) |
package.json |
Project metadata, scripts, dependencies |
bun.lock |
Lockfile (text-based, replaces package-lock.json) |
tsconfig.json |
TypeScript configuration (Bun respects this) |
Key bunfig.toml Sections
[install]
linker = "hoisted" # or "isolated" for strict dependency isolation
dev = true # install devDependencies
optional = true # install optionalDependencies
peer = true # install peerDependencies
[test]
root = "."
coverage = false
coverageThreshold = 0.9
[run]
shell = "system" # or "bun" for Bun's shell
bun = true # alias node to bun in scripts
File Type Support
Bun natively transpiles and executes:
.js,.jsx— JavaScript and JSX.ts,.tsx— TypeScript and TSX.json,.jsonc,.toml,.yaml— Data files (parsed at build time).html— HTML with asset bundling.css— CSS bundling
Decision Guidance
| Scenario | Use | Why |
|---|---|---|
| Package installation | bun install vs npm install |
Bun is 25x faster, uses global cache, supports workspaces |
| Linker strategy | --linker isolated vs --linker hoisted |
Isolated prevents phantom dependencies; hoisted is traditional npm behavior |
| Build target | --target browser vs --target bun vs --target node |
Browser for web apps, bun for server code, node for Node.js compatibility |
| Module format | --format esm vs --format cjs |
ESM is default; use CJS for CommonJS compatibility |
| Watch mode | --watch vs manual restart |
Use --watch for development; Bun uses OS-native file watchers (fast) |
| Test execution | --concurrent vs sequential |
Concurrent for independent tests; sequential for tests with shared state |
| Bundling | bun build vs Bun.build() API |
CLI for simple builds; API for programmatic control and in-memory bundling |
Workflow
1. Initialize a Project
bun init my-app
cd my-app
Choose template: Blank, React, or Library. Creates package.json, tsconfig.json, bunfig.toml.
2. Install Dependencies
bun install
# or add specific packages
bun add react
bun add -d @types/node typescript
Generates bun.lock lockfile. Use --frozen-lockfile in CI for reproducible builds.
3. Write Code
Create .ts, .tsx, .js, or .jsx files. Bun transpiles on the fly.
4. Run Code
bun run index.ts
# or with watch mode
bun --watch run index.ts
5. Add Scripts to package.json
{
"scripts": {
"dev": "bun --watch run src/index.ts",
"build": "bun build ./src/index.tsx --outdir ./dist",
"test": "bun test",
"start": "bun run dist/index.js"
}
}
6. Run Scripts
bun run dev
bun run build
bun run test
7. Test
# Write tests in *.test.ts or *.spec.ts
bun test
bun test --watch
bun test --coverage
8. Bundle for Production
bun build ./src/index.tsx --outdir ./dist --minify
# or for a server
bun build ./src/server.ts --outdir ./dist --target bun --minify
9. Deploy
Commit bun.lock to version control. In CI, use bun ci (equivalent to bun install --frozen-lockfile).
Common Gotchas
- Watch mode flag placement: Use
bun --watch run dev, notbun run dev --watch. Flags after the script name are passed to the script itself. - Lifecycle scripts: Bun does not execute
postinstallscripts for security. Add packages totrustedDependenciesinpackage.jsonto allow them. - Node.js compatibility: Bun aims for Node.js compatibility but is not 100% complete. Check
/runtime/nodejs-compatfor current status. - TypeScript errors in Bun global: Install
@types/bunand configuretsconfig.jsonwith"lib": ["ESNext"]and"module": "Preserve". - Module resolution: Bun supports both ESM and CommonJS. Use
importfor ESM (recommended) orrequire()for CommonJS. - Bundler is not a type checker: Use
tscseparately for type checking and.d.tsgeneration;bun buildonly transpiles. - Auto-install disabled in production: Set
install.auto = "disable"inbunfig.tomlfor production environments. - Phantom dependencies: Use
--linker isolatedto prevent accidental imports of transitive dependencies. - Environment variables: Bun auto-loads
.env,.env.local,.env.[NODE_ENV]. Disable withenv = falseinbunfig.toml. - Minification by default for bun target: When
target: "bun", identifiers are minified by default; useminify: falseto disable.
Verification Checklist
Before submitting work with Bun:
- Run
bun installto ensure dependencies are locked - Run
bun testto verify all tests pass - Run
bun run build(or your build script) and verify output indist/or configuredoutdir - Test the built output:
bun run dist/index.jsornode dist/index.js(if targeting Node.js) - Check
bun.lockis committed to version control - Verify
bunfig.tomlhas correct configuration for your environment (dev vs. production) - Run
bun run --filter <pattern> <script>in monorepos to test workspace scripts - For HTTP servers, test with
curlor browser:curl http://localhost:3000 - For bundled apps, check bundle size:
ls -lh dist/ - Verify no console errors or warnings in test output
- If using TypeScript, ensure
tsconfig.jsonis properly configured
Resources
- Comprehensive navigation: https://bun.com/docs/llms.txt — Full page-by-page listing for agent navigation
- Runtime documentation: https://bun.com/docs/runtime — Execute files, scripts, and use Bun APIs
- Package manager: https://bun.com/docs/pm/cli/install — Install, add, remove, and manage dependencies
- Test runner: https://bun.com/docs/test — Write and run Jest-compatible tests
- Bundler: https://bun.com/docs/bundler — Bundle for browsers and servers
For additional documentation and navigation, see: https://bun.com/docs/llms.txt