bun-package-manager
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBun Package Manager
Bun Package Manager
Use this skill when managing dependencies with Bun's package manager, which is significantly faster than npm, yarn, and pnpm while maintaining compatibility.
当你使用Bun包管理器管理依赖时可使用本技能,Bun包管理器在保持兼容性的同时,速度明显快于npm、yarn和pnpm。
Key Concepts
核心概念
Installing Dependencies
安装依赖
Bun's package manager is drop-in compatible with npm:
bash
undefinedBun包管理器可直接替代npm:
bash
undefinedInstall all dependencies
Install all dependencies
bun install
bun install
Add a dependency
Add a dependency
bun add express
bun add -d typescript # Dev dependency
bun add -g cowsay # Global install
bun add express
bun add -d typescript # Dev dependency
bun add -g cowsay # Global install
Add specific version
Add specific version
bun add react@18.2.0
bun add react@18.2.0
Install from different sources
Install from different sources
bun add git@github.com:user/repo.git
bun add ./local-package
undefinedbun add git@github.com:user/repo.git
bun add ./local-package
undefinedRemoving Dependencies
移除依赖
bash
undefinedbash
undefinedRemove a dependency
Remove a dependency
bun remove express
bun remove express
Remove dev dependency
Remove dev dependency
bun remove -d typescript
undefinedbun remove -d typescript
undefinedUpdating Dependencies
更新依赖
bash
undefinedbash
undefinedUpdate all dependencies
Update all dependencies
bun update
bun update
Update specific package
Update specific package
bun update react
bun update react
Update to latest (ignoring semver)
Update to latest (ignoring semver)
bun update react --latest
undefinedbun update react --latest
undefinedRunning Scripts
运行脚本
Execute package.json scripts:
bash
undefined执行package.json中的脚本:
bash
undefinedRun a script
Run a script
bun run dev
bun run build
bun run test
bun run dev
bun run build
bun run test
Short form (if no file conflict)
Short form (if no file conflict)
bun dev
bun build
bun test
undefinedbun dev
bun build
bun test
undefinedBest Practices
最佳实践
Use bun.lockb
使用bun.lockb
Bun's binary lockfile is faster and more reliable:
bash
undefinedBun的二进制锁文件速度更快且更可靠:
bash
undefinedGenerate lockfile
Generate lockfile
bun install
bun install
Commit bun.lockb to version control
Commit bun.lockb to version control
git add bun.lockb
undefinedgit add bun.lockb
undefinedWorkspaces
工作区
Manage monorepos with workspaces:
json
// package.json
{
"name": "my-monorepo",
"workspaces": ["packages/*", "apps/*"]
}bash
undefined使用工作区管理单仓库项目:
json
// package.json
{
"name": "my-monorepo",
"workspaces": ["packages/*", "apps/*"]
}bash
undefinedInstall all workspace dependencies
Install all workspace dependencies
bun install
bun install
Run script in specific workspace
Run script in specific workspace
bun --filter my-package run build
bun --filter my-package run build
Run script in all workspaces
Run script in all workspaces
bun --filter '*' run test
undefinedbun --filter '*' run test
undefinedPackage.json Configuration
Package.json配置
Configure Bun-specific options:
json
{
"name": "my-app",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "bun run --hot src/index.ts",
"build": "bun build src/index.ts --outdir dist",
"start": "bun run dist/index.js",
"test": "bun test"
},
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"@types/express": "^4.17.0",
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}配置Bun专属选项:
json
{
"name": "my-app",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "bun run --hot src/index.ts",
"build": "bun build src/index.ts --outdir dist",
"start": "bun run dist/index.js",
"test": "bun test"
},
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"@types/express": "^4.17.0",
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}TypeScript Configuration
TypeScript配置
Set up proper TypeScript support:
json
// tsconfig.json
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"types": ["bun-types"]
}
}设置完善的TypeScript支持:
json
// tsconfig.json
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"types": ["bun-types"]
}
}Using Trusted Dependencies
使用可信依赖
Configure trusted dependencies for faster installs:
bash
undefined配置可信依赖以加快安装速度:
bash
undefinedAdd trusted dependency
Add trusted dependency
bun pm trust @prisma/client
bun pm trust @prisma/client
Install without lifecycle scripts (faster)
Install without lifecycle scripts (faster)
bun install --production --frozen-lockfile
undefinedbun install --production --frozen-lockfile
undefinedCommon Patterns
常见模式
Migration from npm/yarn/pnpm
从npm/yarn/pnpm迁移
bash
undefinedbash
undefinedRemove old lockfiles
Remove old lockfiles
rm package-lock.json yarn.lock pnpm-lock.yaml
rm package-lock.json yarn.lock pnpm-lock.yaml
Install with Bun
Install with Bun
bun install
bun install
Update scripts (optional)
Update scripts (optional)
Change "npm run" to "bun run"
Change "npm run" to "bun run"
Change "npx" to "bunx"
Change "npx" to "bunx"
undefinedundefinedPrivate Package Registry
私有包仓库
Configure private registry:
bash
undefined配置私有仓库:
bash
undefinedSet registry
Set registry
bun config set registry https://registry.example.com
bun config set registry https://registry.example.com
Set scoped registry
Set scoped registry
bun config set @myorg:registry https://registry.example.com
bun config set @myorg:registry https://registry.example.com
Set auth token
Set auth token
bun config set //registry.example.com/:_authToken YOUR_TOKEN
undefinedbun config set //registry.example.com/:_authToken YOUR_TOKEN
undefinedCI/CD Installation
CI/CD安装
Optimize for CI environments:
bash
undefined针对CI环境优化:
bash
undefinedFast, frozen lockfile install
Fast, frozen lockfile install
bun install --frozen-lockfile --production
bun install --frozen-lockfile --production
No save (don't update lockfile)
No save (don't update lockfile)
bun install --no-save
undefinedbun install --no-save
undefinedDevelopment Workflow
开发工作流
bash
undefinedbash
undefinedInstall dependencies
Install dependencies
bun install
bun install
Run dev server with hot reload
Run dev server with hot reload
bun --hot run src/index.ts
bun --hot run src/index.ts
Run tests in watch mode
Run tests in watch mode
bun test --watch
bun test --watch
Build for production
Build for production
bun run build
undefinedbun run build
undefinedMonorepo Scripts
单仓库脚本
json
// Root package.json
{
"scripts": {
"dev": "bun --filter '*' run dev",
"build": "bun --filter '*' run build",
"test": "bun --filter '*' run test",
"lint": "bun --filter '*' run lint"
}
}
// Package in workspace
{
"name": "@myorg/shared",
"scripts": {
"dev": "bun run --hot src/index.ts",
"build": "bun build src/index.ts --outdir dist",
"test": "bun test"
}
}json
// Root package.json
{
"scripts": {
"dev": "bun --filter '*' run dev",
"build": "bun --filter '*' run build",
"test": "bun --filter '*' run test",
"lint": "bun --filter '*' run lint"
}
}
// Package in workspace
{
"name": "@myorg/shared",
"scripts": {
"dev": "bun run --hot src/index.ts",
"build": "bun build src/index.ts --outdir dist",
"test": "bun test"
}
}Link Local Packages
链接本地包
bash
undefinedbash
undefinedIn the package you want to link
In the package you want to link
bun link
bun link
In the project using the package
In the project using the package
bun link @myorg/my-package
bun link @myorg/my-package
Unlink
Unlink
bun unlink @myorg/my-package
undefinedbun unlink @myorg/my-package
undefinedAnti-Patterns
反模式
Don't Mix Package Managers
不要混合使用包管理器
bash
undefinedbash
undefinedBad - Mixing package managers
Bad - Mixing package managers
npm install react
bun add express
yarn add vue
npm install react
bun add express
yarn add vue
Good - Use one package manager
Good - Use one package manager
bun add react express vue
undefinedbun add react express vue
undefinedDon't Commit node_modules
不要提交node_modules
bash
undefinedbash
undefinedBad - Committing dependencies
Bad - Committing dependencies
git add node_modules
git add node_modules
Good - Use lockfile
Good - Use lockfile
git add bun.lockb
echo "node_modules" >> .gitignore
undefinedgit add bun.lockb
echo "node_modules" >> .gitignore
undefinedDon't Install Packages Globally Unnecessarily
不要不必要地全局安装包
bash
undefinedbash
undefinedBad - Global install for project dependency
Bad - Global install for project dependency
bun add -g typescript
bun add -g typescript
Good - Install as dev dependency
Good - Install as dev dependency
bun add -d typescript
bun add -d typescript
Use bunx for one-off commands
Use bunx for one-off commands
bunx tsc --version
undefinedbunx tsc --version
undefinedDon't Skip Lockfile in CI
CI环境中不要跳过锁文件
bash
undefinedbash
undefinedBad - Updating dependencies in CI
Bad - Updating dependencies in CI
bun install
bun install
Good - Use frozen lockfile
Good - Use frozen lockfile
bun install --frozen-lockfile
undefinedbun install --frozen-lockfile
undefinedDon't Ignore Peer Dependencies
不要忽略对等依赖
bash
undefinedbash
undefinedBad - Ignoring peer dependency warnings
Bad - Ignoring peer dependency warnings
bun add react-dom
bun add react-dom
Warning: react is a peer dependency of react-dom
Warning: react is a peer dependency of react-dom
Good - Install peer dependencies
Good - Install peer dependencies
bun add react react-dom
undefinedbun add react react-dom
undefinedPerformance Tips
性能技巧
Faster Installs
更快的安装速度
bash
undefinedbash
undefinedUse binary lockfile
Use binary lockfile
bun install # Automatically uses bun.lockb
bun install # Automatically uses bun.lockb
Skip optional dependencies
Skip optional dependencies
bun install --no-optional
bun install --no-optional
Production install (skip devDependencies)
Production install (skip devDependencies)
bun install --production
bun install --production
Frozen lockfile (don't update)
Frozen lockfile (don't update)
bun install --frozen-lockfile
undefinedbun install --frozen-lockfile
undefinedCache Management
缓存管理
bash
undefinedbash
undefinedClear Bun cache
Clear Bun cache
bun pm cache rm
bun pm cache rm
Check cache size
Check cache size
bun pm cache
undefinedbun pm cache
undefinedParallel Installation
并行安装
Bun installs packages in parallel automatically, making it significantly faster than npm/yarn/pnpm.
Bun会自动并行安装包,这使其速度明显快于npm/yarn/pnpm。
Troubleshooting
故障排除
Check Package Info
查看包信息
bash
undefinedbash
undefinedView package details
View package details
bun pm ls express
bun pm ls express
View all dependencies
View all dependencies
bun pm ls
bun pm ls
Check for updates
Check for updates
bun outdated
undefinedbun outdated
undefinedFixing Lockfile Issues
修复锁文件问题
bash
undefinedbash
undefinedRegenerate lockfile
Regenerate lockfile
rm bun.lockb
bun install
rm bun.lockb
bun install
Verify lockfile
Verify lockfile
bun install --frozen-lockfile
undefinedbun install --frozen-lockfile
undefinedRelated Skills
相关技能
- bun-runtime: Understanding Bun's runtime for dependency usage
- bun-testing: Testing with Bun and managing test dependencies
- bun-bundler: Building projects with installed dependencies
- bun-runtime: 了解Bun运行时的依赖使用方式
- bun-testing: 使用Bun进行测试并管理测试依赖
- bun-bundler: 使用已安装的依赖构建项目