Bun Cheatsheet
Bun is an ultra-fast all-in-one JavaScript runtime, bundler, and package manager powered by JavaScriptCore instead of V8. It aims to replace Node.js, npm, npx, and esbuild with a single tool.
Quick Start
``bash
# Run a script
bun run index.ts
# Watch mode — auto-restart on changes bun run --watch index.ts
# Dev mode with hot reload bun --hot server.ts
# Start dev server (from package.json scripts) bun dev
# Install dependencies bun install
# Add a package bun add zod
# Run a one-off npm package bun x cowsay 'hello bun'
# Run tests
bun test
`
Install & Setup
`bash
bun --version # Show version
bun upgrade # Upgrade to latest
bun --help # Show help
`
Basic Usage
`bash
bun run index.ts # Run a file
bun run --watch index.ts # Watch mode (auto-restart)
bun --hot server.ts # Hot reload (no restart needed)
bun --smol index.ts # Low memory mode
bun . # Auto-detect entry point
bun dev # Run dev script from package.json
bun start # Run start script from package.json
`
Package Management
`bash
bun install # Install all dependencies
bun add zod # Add a dependency
bun add -d @types/bun # Add a dev dependency
bun add --exact zod@3.23.0 # Add with exact version
bun remove zod # Remove a dependency
bun update # Update all dependencies
bun pm cache # View/clear package cache
bun pm ls # List installed dependencies
`
Testing & Debug
`bash
bun test # Run all tests
bun test --watch # Watch mode
bun test --coverage # With coverage report
bun test --bail # Stop on first failure
bun test --timeout 10000 # Per-test timeout (ms)
`
Bundler
`bash
bun build ./src/index.ts --outdir ./dist # Bundle to single file
bun build --target bun ./src/index.ts --outdir ./dist # Target Bun runtime
bun build --minify ./src/index.ts --outdir ./dist # Minify output
bun build --splitting ./src/pages/*.tsx --outdir ./dist # Code splitting
bun build --sourcemap=external ./src/index.ts --outdir ./dist # External source maps
`
Advanced
`bash
bun x cowsay 'hello' # Run npm package without installing
bun create next-app # Create project from template
bun init # Initialize new project
bun link # Register local package globally
bun patch lodash # Patch an installed npm package
bun publish # Publish to npm registry
bun fig # Generate Fig autocomplete spec
`
FAQ
### How do I configure Bun?
Create a bunfig.toml in your project root (~/.bunfig.toml also works). Example:
`toml
[install]
cache = true
production = false
frozenLockfile = false
registry = "https://registry.npmjs.org/"
[test]
preload = "./test/setup.ts"
`
### How does bun.lock work?
Bun uses a binary lockfile format (bun.lock) instead of JSON. It's faster to parse and smaller on disk. You can convert to/from package-lock.json with plugins. Commit it to version control. To regenerate: delete both node_modules and bun.lock, then run bun install.
### How do I use Env variables with Bun?
Bun automatically loads .env files — no need for dotenv. It reads .env, .env.local, .env.production (based on NODE_ENV). Access via process.env:
`typescript
const db = process.env.DATABASE_URL;
`
### Can I use Bun with existing Node.js projects?
Yes! Run bun install in any Node.js project to replace npm/yarn. For most projects, bun run dev / bun run build` works since Bun runs package.json scripts and is compatible with the Node.js API. Some projects may need minor adjustments for edge-case Node.js internals.
Install & Setup(3)
| Command | Level | ||
|---|---|---|---|
bun --versionShow Bun version | Basic | bun --version | |
bun upgradeUpgrade Bun to the latest version | Basic | bun upgrade | |
bun --helpShow Bun help | Basic | bun --help |
Basic Usage(7)
| Command | Level | ||
|---|---|---|---|
bun runRun a JavaScript or TypeScript file | Basic | bun run index.ts | |
bun run --watchWatch mode — auto-restart on file changes | Intermediate | bun run --watch index.ts | |
bun --hotHot reload — apply changes without restart | Intermediate | bun --hot server.ts | |
bun --smolLow memory mode for constrained environments | Intermediate | bun --smol index.ts | |
bun .Auto-detect and run entry point in current dir | Basic | bun . | |
bun devStart dev mode with watch and hot reload | Basic | bun dev | |
bun startRun the start script from package.json | Basic | bun start |
Package Management(9)
| Command | Level | ||
|---|---|---|---|
bun installInstall all dependencies from package.json | Basic | bun install | |
bun addAdd a dependency to the project | Basic | bun add zod | |
bun add -dAdd a dev dependency | Basic | bun add -d @types/bun | |
bun add --exactAdd dependency with exact version pinning | Intermediate | bun add --exact zod@3.23.0 | |
bun removeRemove a dependency | Basic | bun remove zod | |
bun updateUpdate all dependencies to latest compatible versions | Intermediate | bun update | |
bun pm cacheView or clear the package cache | Intermediate | bun pm cache | |
bun pm lsList installed direct dependencies | Intermediate | bun pm ls | |
bun bun.lockView the generated lock file (binary format) | Expert | cat bun.lock |
Testing & Debug(5)
| Command | Level | ||
|---|---|---|---|
bun testRun tests (compatible with Jest and Node tests) | Basic | bun test | |
bun test --watchRun tests in watch mode | Intermediate | bun test --watch | |
bun test --coverageRun tests with coverage report | Intermediate | bun test --coverage | |
bun test --bailStop on first test failure | Basic | bun test --bail | |
bun test --timeout 10000Set per-test timeout in milliseconds | Intermediate | bun test --timeout 10000 |
Bundler(5)
| Command | Level | ||
|---|---|---|---|
bun buildBundle JS/TS into a single file | Intermediate | bun build ./src/index.ts --outdir ./dist | |
bun build --target bunBundle targeting Bun runtime | Intermediate | bun build --target bun ./src/index.ts --outdir ./dist | |
bun build --minifyBundle with minification | Intermediate | bun build --minify ./src/index.ts --outdir ./dist | |
bun build --splittingEnable code splitting for multiple entry points | Expert | bun build --splitting ./src/pages/*.tsx --outdir ./dist | |
bun build --sourcemap=externalBundle with external source maps | Intermediate | bun build --sourcemap=external ./src/index.ts --outdir ./dist |
Advanced(7)
| Command | Level | ||
|---|---|---|---|
bun xRun an npm package without installing | Basic | bun x cowsay 'hello' | |
bun createCreate a new project from a template | Basic | bun create next-app | |
bun initInitialize a new project with package.json | Basic | bun init | |
bun linkRegister a local package for global linking | Intermediate | bun link | |
bun patchPatch an installed npm package | Expert | bun patch lodash | |
bun publishPublish package to npm registry | Intermediate | bun publish | |
bun figGenerate Fig autocomplete specification | Expert | bun fig |