bun
Always use when working with Bun projects
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Bun Cheatsheet
Fast all-in-one JavaScript runtime, bundler, test runner, and package manager.
Key Commands
| Command | Description |
|---|---|
bun init |
Create new project |
bun install |
Install dependencies |
bun add <pkg> |
Add package |
bun remove <pkg> |
Remove package |
bun update |
Update dependencies |
bun run <script> |
Run script |
bun test |
Run tests |
bun build |
Bundle code |
bunx <cmd> |
Run package binary |
bun audit |
Check for vulnerabilities |
bun outdated |
Show outdated packages |
bun why <pkg> |
Explain why package is installed |
bun publish |
Publish to npm registry |
bun patch <pkg> |
Prepare package for patching |
Project Setup
bun init # Basic project
bun init --react # React project
bun init --react=tailwind # React + Tailwind
bun init --react=shadcn # React + shadcn/ui
Package Management
bun add <pkg> # Production dep
bun add -d <pkg> # Dev dependency
bun add --optional <pkg> # Optional dep
bun add -g <pkg> # Global install
bun remove <pkg> # Remove package
bun update # Update all
bun outdated # Show outdated
bun audit # Security audit
bun why <pkg> # Why installed
bun info <pkg> # Show package metadata
bun publish # Publish to npm
bun patch <pkg> # Patch a package
bun link # Link local package
bun unlink # Unlink local package
Running Code
bun run ./file.ts # Execute file
bun run dev # Run script
bun -e "code" # Eval code
bun exec ./script.sh # Run shell script
# Long-running/watch mode (use tmux for background)
tmux new -d -s dev 'bun run --watch ./file.ts'
tmux new -d -s hot 'bun run --hot ./file.ts'
tmux new -d -s repl 'bun repl'
Testing
bun test # Run all tests
bun test --coverage # With coverage
bun test -t "pattern" # Filter by name
bun test --bail 3 # Stop after N failures
bun test -u # Update snapshots
# Watch mode (use tmux for background)
tmux new -d -s test 'bun test --watch'
Building
bun build ./src/index.ts # Bundle
bun build --production ./src/index.ts # Minified
bun build --target=node ./src/index.ts # Node target
bun build --compile ./cli.ts # Executable
bun build --splitting ./src/index.ts # Code splitting
Configuration (bunfig.toml)
[install]
linker = "hoisted" # or "isolated"
minimumReleaseAge = 259200 # Security (3 days)
optional = true
[test]
timeout = 5000
coverage = { reporter = ["text"] }
[build]
minify = true
sourcemap = "external"
Workspaces
Root package.json:
{
"workspaces": ["packages/*"],
"catalog": {
"react": "^18.0.0",
"typescript": "^5.0.0"
}
}
Commands:
bun run --workspaces test # Run in all workspaces
bun add <pkg> --filter <ws> # Add to specific workspace
bun remove <pkg> --filter <ws> # Remove from workspace
Environment Variables
bun run --env-file=.env dev # Load .env
process.env.VAR # Access in code
Tips
- Use
bunxfor one-off CLIs (auto-installs) - Commit
bun.lockfor reproducible installs -
--frozen-lockfilein CI to prevent changes -
bun run -iauto-installs missing deps - Standalone executables:
bun build --compile - Use tmux for watch/hot modes:
tmux new -d -s dev 'bun run --watch'
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!