bun-package-manager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Bun 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
undefined
Bun包管理器可直接替代npm:
bash
undefined

Install 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
undefined
bun add git@github.com:user/repo.git bun add ./local-package
undefined

Removing Dependencies

移除依赖

bash
undefined
bash
undefined

Remove a dependency

Remove a dependency

bun remove express
bun remove express

Remove dev dependency

Remove dev dependency

bun remove -d typescript
undefined
bun remove -d typescript
undefined

Updating Dependencies

更新依赖

bash
undefined
bash
undefined

Update 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
undefined
bun update react --latest
undefined

Running Scripts

运行脚本

Execute package.json scripts:
bash
undefined
执行package.json中的脚本:
bash
undefined

Run 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
undefined
bun dev bun build bun test
undefined

Best Practices

最佳实践

Use bun.lockb

使用bun.lockb

Bun's binary lockfile is faster and more reliable:
bash
undefined
Bun的二进制锁文件速度更快且更可靠:
bash
undefined

Generate lockfile

Generate lockfile

bun install
bun install

Commit bun.lockb to version control

Commit bun.lockb to version control

git add bun.lockb
undefined
git add bun.lockb
undefined

Workspaces

工作区

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
undefined

Install 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
undefined
bun --filter '*' run test
undefined

Package.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
undefined

Add 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
undefined
bun install --production --frozen-lockfile
undefined

Common Patterns

常见模式

Migration from npm/yarn/pnpm

从npm/yarn/pnpm迁移

bash
undefined
bash
undefined

Remove 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"

undefined
undefined

Private Package Registry

私有包仓库

Configure private registry:
bash
undefined
配置私有仓库:
bash
undefined

Set 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
undefined
bun config set //registry.example.com/:_authToken YOUR_TOKEN
undefined

CI/CD Installation

CI/CD安装

Optimize for CI environments:
bash
undefined
针对CI环境优化:
bash
undefined

Fast, 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
undefined
bun install --no-save
undefined

Development Workflow

开发工作流

bash
undefined
bash
undefined

Install 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
undefined
bun run build
undefined

Monorepo 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
undefined
bash
undefined

In 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
undefined
bun unlink @myorg/my-package
undefined

Anti-Patterns

反模式

Don't Mix Package Managers

不要混合使用包管理器

bash
undefined
bash
undefined

Bad - 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
undefined
bun add react express vue
undefined

Don't Commit node_modules

不要提交node_modules

bash
undefined
bash
undefined

Bad - 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
undefined
git add bun.lockb echo "node_modules" >> .gitignore
undefined

Don't Install Packages Globally Unnecessarily

不要不必要地全局安装包

bash
undefined
bash
undefined

Bad - 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
undefined
bunx tsc --version
undefined

Don't Skip Lockfile in CI

CI环境中不要跳过锁文件

bash
undefined
bash
undefined

Bad - 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
undefined
bun install --frozen-lockfile
undefined

Don't Ignore Peer Dependencies

不要忽略对等依赖

bash
undefined
bash
undefined

Bad - 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
undefined
bun add react react-dom
undefined

Performance Tips

性能技巧

Faster Installs

更快的安装速度

bash
undefined
bash
undefined

Use 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
undefined
bun install --frozen-lockfile
undefined

Cache Management

缓存管理

bash
undefined
bash
undefined

Clear Bun cache

Clear Bun cache

bun pm cache rm
bun pm cache rm

Check cache size

Check cache size

bun pm cache
undefined
bun pm cache
undefined

Parallel Installation

并行安装

Bun installs packages in parallel automatically, making it significantly faster than npm/yarn/pnpm.
Bun会自动并行安装包,这使其速度明显快于npm/yarn/pnpm。

Troubleshooting

故障排除

Check Package Info

查看包信息

bash
undefined
bash
undefined

View 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
undefined
bun outdated
undefined

Fixing Lockfile Issues

修复锁文件问题

bash
undefined
bash
undefined

Regenerate lockfile

Regenerate lockfile

rm bun.lockb bun install
rm bun.lockb bun install

Verify lockfile

Verify lockfile

bun install --frozen-lockfile
undefined
bun install --frozen-lockfile
undefined

Related 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: 使用已安装的依赖构建项目