Skip to main content

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.

Updated: 2026-07-20·36 commands

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)

CommandLevel
bun --version
Show Bun version
Basic
bun upgrade
Upgrade Bun to the latest version
Basic
bun --help
Show Bun help
Basic

Basic Usage(7)

CommandLevel
bun run
Run a JavaScript or TypeScript file
Basic
bun run --watch
Watch mode — auto-restart on file changes
Intermediate
bun --hot
Hot reload — apply changes without restart
Intermediate
bun --smol
Low memory mode for constrained environments
Intermediate
bun .
Auto-detect and run entry point in current dir
Basic
bun dev
Start dev mode with watch and hot reload
Basic
bun start
Run the start script from package.json
Basic

Package Management(9)

CommandLevel
bun install
Install all dependencies from package.json
Basic
bun add
Add a dependency to the project
Basic
bun add -d
Add a dev dependency
Basic
bun add --exact
Add dependency with exact version pinning
Intermediate
bun remove
Remove a dependency
Basic
bun update
Update all dependencies to latest compatible versions
Intermediate
bun pm cache
View or clear the package cache
Intermediate
bun pm ls
List installed direct dependencies
Intermediate
bun bun.lock
View the generated lock file (binary format)
Expert

Testing & Debug(5)

CommandLevel
bun test
Run tests (compatible with Jest and Node tests)
Basic
bun test --watch
Run tests in watch mode
Intermediate
bun test --coverage
Run tests with coverage report
Intermediate
bun test --bail
Stop on first test failure
Basic
bun test --timeout 10000
Set per-test timeout in milliseconds
Intermediate

Bundler(5)

CommandLevel
bun build
Bundle JS/TS into a single file
Intermediate
bun build --target bun
Bundle targeting Bun runtime
Intermediate
bun build --minify
Bundle with minification
Intermediate
bun build --splitting
Enable code splitting for multiple entry points
Expert
bun build --sourcemap=external
Bundle with external source maps
Intermediate

Advanced(7)

CommandLevel
bun x
Run an npm package without installing
Basic
bun create
Create a new project from a template
Basic
bun init
Initialize a new project with package.json
Basic
bun link
Register a local package for global linking
Intermediate
bun patch
Patch an installed npm package
Expert
bun publish
Publish package to npm registry
Intermediate
bun fig
Generate Fig autocomplete specification
Expert

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-20.