Bun包管理器技能Skill bun-package-manager

Bun包管理器技能专注于使用Bun的包管理工具高效管理JavaScript和TypeScript项目的依赖项,覆盖安装、移除、更新、脚本执行、工作区管理、锁文件优化、性能技巧、迁移和CI/CD环境。关键词:Bun, 包管理, 依赖管理, JavaScript, TypeScript, 性能优化, DevOps。

DevOps 0 次安装 0 次浏览 更新于 3/25/2026

name: bun-package-manager user-invocable: false description: 使用Bun的包管理器管理依赖项时使用。涵盖安装包、工作区、锁文件,以及从npm/yarn/pnpm迁移到Bun。 allowed-tools:

  • 读取
  • 写入
  • 编辑
  • Bash
  • Grep
  • Glob

Bun包管理器

使用此技能当使用Bun的包管理器管理依赖项时,Bun比npm、yarn和pnpm显著更快,同时保持兼容性。

关键概念

安装依赖项

Bun的包管理器与npm完全兼容:

# 安装所有依赖项
bun install

# 添加依赖项
bun add express
bun add -d typescript  # 开发依赖项
bun add -g cowsay      # 全局安装

# 添加特定版本
bun add react@18.2.0

# 从不同源安装
bun add git@github.com:user/repo.git
bun add ./local-package

移除依赖项

# 移除依赖项
bun remove express

# 移除开发依赖项
bun remove -d typescript

更新依赖项

# 更新所有依赖项
bun update

# 更新特定包
bun update react

# 更新到最新(忽略语义版本)
bun update react --latest

运行脚本

执行package.json脚本:

# 运行脚本
bun run dev
bun run build
bun run test

# 简短形式(如果没有文件冲突)
bun dev
bun build
bun test

最佳实践

使用bun.lockb

Bun的二进制锁文件更快更可靠:

# 生成锁文件
bun install

# 提交bun.lockb到版本控制
git add bun.lockb

工作区

使用工作区管理monorepo:

// package.json
{
  "name": "my-monorepo",
  "workspaces": ["packages/*", "apps/*"]
}
# 安装所有工作区依赖项
bun install

# 在特定工作区运行脚本
bun --filter my-package run build

# 在所有工作区运行脚本
bun --filter '*' run test

Package.json配置

配置Bun特定选项:

{
  "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配置

设置适当的TypeScript支持:

// 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"]
  }
}

使用可信赖的依赖项

配置可信赖的依赖项以加快安装:

# 添加可信赖依赖项
bun pm trust @prisma/client

# 安装时不运行生命周期脚本(更快)
bun install --production --frozen-lockfile

常见模式

从npm/yarn/pnpm迁移

# 移除旧的锁文件
rm package-lock.json yarn.lock pnpm-lock.yaml

# 使用Bun安装
bun install

# 更新脚本(可选)
# 将"npm run"改为"bun run"
# 将"npx"改为"bunx"

私有包注册表

配置私有注册表:

# 设置注册表
bun config set registry https://registry.example.com

# 设置作用域注册表
bun config set @myorg:registry https://registry.example.com

# 设置认证令牌
bun config set //registry.example.com/:_authToken YOUR_TOKEN

CI/CD安装

为CI环境优化:

# 快速、冻结锁文件安装
bun install --frozen-lockfile --production

# 不保存(不更新锁文件)
bun install --no-save

开发工作流

# 安装依赖项
bun install

# 运行带有热重载的开发服务器
bun --hot run src/index.ts

# 在监视模式下运行测试
bun test --watch

# 为生产构建
bun run build

Monorepo脚本

// 根package.json
{
  "scripts": {
    "dev": "bun --filter '*' run dev",
    "build": "bun --filter '*' run build",
    "test": "bun --filter '*' run test",
    "lint": "bun --filter '*' run lint"
  }
}

// 工作区中的包
{
  "name": "@myorg/shared",
  "scripts": {
    "dev": "bun run --hot src/index.ts",
    "build": "bun build src/index.ts --outdir dist",
    "test": "bun test"
  }
}

链接本地包

# 在要链接的包中
bun link

# 在使用包的项目中
bun link @myorg/my-package

# 取消链接
bun unlink @myorg/my-package

反模式

不要混合包管理器

# 坏 - 混合包管理器
npm install react
bun add express
yarn add vue

# 好 - 使用一个包管理器
bun add react express vue

不要提交node_modules

# 坏 - 提交依赖项
git add node_modules

# 好 - 使用锁文件
git add bun.lockb
echo "node_modules" >> .gitignore

不要不必要地全局安装包

# 坏 - 为项目依赖项全局安装
bun add -g typescript

# 好 - 作为开发依赖项安装
bun add -d typescript

# 使用bunx进行一次性命令
bunx tsc --version

不要在CI中跳过锁文件

# 坏 - 在CI中更新依赖项
bun install

# 好 - 使用冻结锁文件
bun install --frozen-lockfile

不要忽略对等依赖项

# 坏 - 忽略对等依赖项警告
bun add react-dom
# 警告: react是react-dom的对等依赖项

# 好 - 安装对等依赖项
bun add react react-dom

性能技巧

更快安装

# 使用二进制锁文件
bun install  # 自动使用bun.lockb

# 跳过可选依赖项
bun install --no-optional

# 生产安装(跳过devDependencies)
bun install --production

# 冻结锁文件(不更新)
bun install --frozen-lockfile

缓存管理

# 清除Bun缓存
bun pm cache rm

# 检查缓存大小
bun pm cache

并行安装

Bun自动并行安装包,使其比npm/yarn/pnpm显著更快。

故障排除

检查包信息

# 查看包详情
bun pm ls express

# 查看所有依赖项
bun pm ls

# 检查更新
bun outdated

修复锁文件问题

# 重新生成锁文件
rm bun.lockb
bun install

# 验证锁文件
bun install --frozen-lockfile

相关技能

  • bun-runtime: 理解Bun运行时以使用依赖项
  • bun-testing: 使用Bun测试和管理测试依赖项
  • bun-bundler: 使用安装的依赖项构建项目