Fast, modern JavaScript/TypeScript development with the Bun runtime, inspired by oven-sh/bun.
When to Use This Skill
Use this skill when:
Starting new JS/TS projects with Bun
Migrating from Node.js to Bun
Optimizing development speed
Using Bun's built-in tools (bundler, test runner)
Troubleshooting Bun-specific issues
1. Getting Started
1.1 Installation
# macOS / Linux
curl -fsSL https://bun.sh/install | bash
# Windows
powershell -c "irm bun.sh/install.ps1 | iex"# Homebrew
brew tap oven-sh/bun
brew install bun
# npm (if needed)
npm install -g bun
# Upgrade
bun upgrade
1.2 Why Bun?
Feature
Bun
Node.js
Startup time
~25ms
~100ms+
Package install
10-100x faster
Baseline
TypeScript
Native
Requires transpiler
JSX
Native
Requires transpiler
Test runner
Built-in
External (Jest, Vitest)
Bundler
Built-in
External (Webpack, esbuild)
2. Project Setup
2.1 Create New Project
# Initialize project
bun init
# Creates:# ├── package.json# ├── tsconfig.json# ├── index.ts# └── README.md# With specific template
bun create <template> <project-name>
bun create react my-app
bun create next my-app
bun create vite my-app
bun create elysia my-api
# Examples
# React app
# Next.js app
# Vite app
# Elysia API
2.2 package.json
{"name":"my-bun-project","version":"1.0.0","module":"index.ts","type":"module","scripts":{"dev":"bun run --watch index.ts","start":"bun run index.ts","test":"bun test","build":"bun build ./index.ts --outdir ./dist","lint":"bunx eslint ."},"devDependencies":{"@types/bun":"latest"},"peerDependencies":{"typescript":"^5.0.0"}}
# Install from package.json
bun install # or 'bun i'# Add dependencies
bun add express # Regular dependency
bun add -d typescript # Dev dependency
bun add -D @types/node # Dev dependency (alias)
bun add --optional pkg # Optional dependency# From specific registry
bun add lodash --registry https://registry.npmmirror.com
# Install specific version
bun add react@18.2.0
bun add react@latest
bun add react@next
# From git
bun add github:user/repo
bun add git+https://github.com/user/repo.git
3.2 Removing & Updating
# Remove package
bun remove lodash
# Update packages
bun update # Update all
bun update lodash # Update specific
bun update --latest # Update to latest (ignore ranges)# Check outdated
bun outdated
3.3 bunx (npx equivalent)
# Execute package binaries
bunx prettier --write .
bunx tsc --init
bunx create-react-app my-app
# With specific version
bunx -p typescript@4.9 tsc --version
# Run without installing
bunx cowsay "Hello from Bun!"
3.4 Lockfile
# bun.lockb is a binary lockfile (faster parsing)# To generate text lockfile for debugging:
bun install --yarn # Creates yarn.lock# Trust existing lockfile
bun install --frozen-lockfile
4. Running Code
4.1 Basic Execution
# Run TypeScript directly (no build step!)
bun run index.ts
# Run JavaScript
bun run index.js
# Run with arguments
bun run server.ts --port 3000
# Run package.json script
bun run dev
bun run build
# Short form (for scripts)
bun dev
bun build
4.2 Watch Mode
# Auto-restart on file changes
bun --watch run index.ts
# With hot reloading
bun --hot run server.ts
4.3 Environment Variables
// .env file is loaded automatically!// Access environment variablesconst apiKey = Bun.env.API_KEY;
const port = Bun.env.PORT ?? "3000";
// Or use process.env (Node.js compatible)const dbUrl = process.env.DATABASE_URL;
# Run with specific env file
bun --env-file=.env.production run index.ts
# Run all tests
bun test# Run specific file
bun test math.test.ts
# Run matching pattern
bun test --grep "adds"# Watch mode
bun test --watch
# With coverage
bun test --coverage
# Timeout
bun test --timeout 5000
# Bundle for production
bun build ./src/index.ts --outdir ./dist
# With options
bun build ./src/index.ts \
--outdir ./dist \
--target browser \
--minify \
--sourcemap
# Create standalone executable
bun build ./src/cli.ts --compile --outfile myapp
# Cross-compile
bun build ./src/cli.ts --compile --target=bun-linux-x64 --outfile myapp-linux
bun build ./src/cli.ts --compile --target=bun-darwin-arm64 --outfile myapp-mac
# With embedded assets
bun build ./src/cli.ts --compile --outfile myapp --embed ./assets
8. Migration from Node.js
8.1 Compatibility
// Most Node.js APIs work out of the boximport fs from"fs";
import path from"path";
import crypto from"crypto";
// process is globalconsole.log(process.cwd());
console.log(process.env.HOME);
// Buffer is globalconst buf = Buffer.from("hello");
// __dirname and __filename workconsole.log(__dirname);
console.log(__filename);
8.2 Common Migration Steps
# 1. Install Bun
curl -fsSL https://bun.sh/install | bash
# 2. Replace package managerrm -rf node_modules package-lock.json
bun install
# 3. Update scripts in package.json# "start": "node index.js" → "start": "bun run index.ts"# "test": "jest" → "test": "bun test"# 4. Add Bun types
bun add -d @types/bun
8.3 Differences from Node.js
// ❌ Node.js specific (may not work)require("module") // Use import insteadrequire.resolve("pkg") // Use import.meta.resolve
__non_webpack_require__ // Not supported// ✅ Bun equivalentsimport pkg from"pkg";
const resolved = import.meta.resolve("pkg");
Bun.resolveSync("pkg", process.cwd());
// ❌ These globals differ
process.hrtime() // Use Bun.nanoseconds()setImmediate() // Use queueMicrotask()// ✅ Bun-specific featuresconst file = Bun.file("./data.txt"); // Fast file APIBun.serve({ port: 3000, fetch: ... }); // Fast HTTP serverBun.password.hash(password); // Built-in hashing
# Always bundle and minify for production
bun build ./src/index.ts --outdir ./dist --minify --target node
# Then run the bundle
bun run ./dist/index.js