bun-best-practices
1
总安装量
1
周安装量
#53802
全站排名
安装命令
npx skills add https://github.com/phrazzld/claude-config --skill bun-best-practices
Agent 安装分布
mcpjam
1
claude-code
1
replit
1
junie
1
windsurf
1
zencoder
1
Skill 文档
Bun Best Practices
Foundational knowledge for Bun adoption decisions. Bun serves two distinct rolesâunderstand when each applies.
Two Distinct Roles
Bun as Package Manager
Replaces pnpm/npm/yarn for dependency management:
bun installâ Install dependencies (fast, binary lockfile)bun add <pkg>â Add dependencybun remove <pkg>â Remove dependencybun --filter <workspace>â Run in specific workspace
Key differences from pnpm:
- Lockfile:
bun.lock(binary) vspnpm-lock.yaml(YAML) - Workspaces: Defined in root
package.json, no separatepnpm-workspace.yaml - Speed: Significantly faster installs due to binary lockfile and caching
Bun as Runtime
Replaces Node.js for executing JavaScript/TypeScript:
bun run script.tsâ Execute TypeScript directly (no tsc)bun testâ Built-in test runner (Jest-compatible API)bun buildâ Bundle for production- Native SQLite, file I/O, HTTP server optimizations
Key differences from Node.js:
- TypeScript: Transpilation, not full tsc checking (use
tsc --noEmitfor types) - APIs: Some Node.js APIs missing or behave differently
- Performance: Often 2-10x faster for I/O-heavy workloads
Decision Tree: When to Use Bun
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Consider Bun When: â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
CLI tools (fast startup, no cold boot) â
â â
Edge functions (lightweight runtime) â
â â
Internal dev tools (team controls deployment) â
â â
New greenfield projects (no legacy constraints) â
â â
Scripts/automation (fast execution) â
â â
Projects with simple dependency trees â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Prefer pnpm When: â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â ï¸ Expo/EAS builds (Node.js required) â
â â ï¸ Vercel serverless (limited Bun support) â
â â ï¸ Complex native module dependencies â
â â ï¸ Team unfamiliar with Bun quirks â
â â ï¸ Production apps needing maximum stability â
â â ï¸ Projects with extensive Node.js API usage â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Hybrid Setup Pattern
Many projects benefit from using both:
monorepo/
âââ apps/
â âââ web/ # Next.js app â pnpm (Vercel deployment)
â âââ mobile/ # Expo app â pnpm (EAS requires Node)
âââ packages/
â âââ shared/ # Shared utilities â either works
âââ tools/
â âââ cli/ # Internal CLI â Bun (fast startup)
âââ scripts/ # Build/deploy scripts â Bun (fast execution)
Rule of thumb:
- External-facing production apps â pnpm (stability)
- Internal tools and scripts â Bun (speed)
Workspace Configuration
pnpm (separate file)
# pnpm-workspace.yaml
packages:
- 'apps/*'
- 'packages/*'
Bun (in package.json)
{
"workspaces": ["apps/*", "packages/*"]
}
Migration note: Remove pnpm-workspace.yaml when switching to Bun; add workspaces to root package.json.
Anti-Patterns
1. Assuming Full Node.js Compatibility
Wrong:
// Assuming all Node.js APIs work identically
import { fork } from 'child_process';
fork('./worker.js'); // May behave differently in Bun
Right:
// Test critical Node.js API usage before migration
// Check: https://bun.sh/docs/runtime/nodejs-apis
2. Mixing Lockfiles
Wrong:
project/
âââ bun.lock
âââ pnpm-lock.yaml # Both present = confusion
âââ package.json
Right:
project/
âââ bun.lock # OR pnpm-lock.yaml, not both
âââ package.json
3. Skipping CI Migration
Wrong:
# Still using pnpm in CI
- uses: pnpm/action-setup@v4
- run: pnpm install
Right:
# Match local tooling
- uses: oven-sh/setup-bun@v2
- run: bun install
4. Ignoring Platform Support
Wrong:
// Deploying to platform that doesn't support Bun runtime
export default async function handler(req, res) {
// Assumes Bun runtime features
}
Right:
// Verify deployment target supports Bun
// Vercel: experimental Bun runtime flag
// Netlify: Node.js only (as of 2025)
// Fly.io: Full Bun support
Performance Benchmarking
Before migrating, benchmark your specific workflows:
# Install time comparison
time pnpm install
time bun install
# Script execution comparison
time pnpm run build
time bun run build
# Test runner comparison
time pnpm test
time bun test
Only migrate if Bun provides measurable benefit for YOUR project.
Related References
references/workspace-config.mdâ Workspace migration detailsreferences/compatibility-matrix.mdâ Framework/tool compatibilityreferences/ci-cd-patterns.mdâ CI/CD configuration patternsreferences/hybrid-setup.mdâ Running Bun + pnpm together
Related Skills
/check-bunâ Audit project for Bun compatibility/fix-bunâ Fix Bun migration issues/bunâ Full Bun migration orchestrator