npm 和 Yarn 是两个最流行的 JavaScript 包管理器,各有优缺点。了解它们的区别和选择合适的工具对于项目开发很重要。
基本介绍
npm
- 发布时间:2010 年
- 开发者:Isaac Z. Schlueter
- 维护者:npm, Inc.(现属 GitHub)
- 默认安装:随 Node.js 一起安装
Yarn
- 发布时间:2016 年
- 开发者:Facebook、Google、Exponent 和 Tilde
- 维护者:Open Collective 社区
- 安装方式:需要单独安装
核心区别
1. 安装速度
npm:
- 早期版本串行安装依赖
- npm 7+ 引入并行安装,速度显著提升
- 使用缓存机制加速重复安装
Yarn:
- 从一开始就支持并行安装
- 通常比 npm 6 及更早版本快
- 与 npm 7+ 速度相当
bash# npm 安装 npm install # Yarn 安装 yarn install
2. 锁文件
npm:
- 使用
package-lock.json - npm 5+ 自动生成
- 记录精确的依赖版本和树结构
Yarn:
- 使用
yarn.lock - 自动生成
- 更详细的锁文件格式
示例:
json// package-lock.json { "name": "my-project", "lockfileVersion": 2, "packages": { "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "integrity": "sha512-..." } } }
yaml# yarn.lock # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 lodash@^4.17.21: version "4.17.21" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#..." integrity sha512-...
3. 命令差异
| 功能 | npm | Yarn |
|---|---|---|
| 安装依赖 | npm install | yarn install |
| 添加依赖 | npm install <pkg> | yarn add <pkg> |
| 添加开发依赖 | npm install <pkg> -D | yarn add <pkg> -D |
| 全局安装 | npm install -g <pkg> | yarn global add <pkg> |
| 更新依赖 | npm update | yarn upgrade |
| 删除依赖 | npm uninstall <pkg> | yarn remove <pkg> |
| 运行脚本 | npm run <script> | yarn run <script> 或 yarn <script> |
| 查看信息 | npm info <pkg> | yarn info <pkg> |
4. 工作区(Workspaces)
npm:
- npm 7+ 原生支持工作区
- 配置简单,与 package.json 集成
json{ "name": "my-monorepo", "workspaces": [ "packages/*" ] }
Yarn:
- Yarn 1+ 支持工作区
- 更成熟的工作区功能
- 支持 Yarn Plug'n'Play(PnP)
json{ "name": "my-monorepo", "private": true, "workspaces": { "packages": [ "packages/*" ] } }
5. 离线模式
npm:
bashnpm install --offline npm install --prefer-offline
Yarn:
bashyarn install --offline yarn install --prefer-offline
Yarn 的离线模式更成熟,缓存管理更好。
6. 输出格式
npm:
- 输出较详细
- 显示安装进度和警告
Yarn:
- 输出更简洁
- 使用 emoji 和颜色
- 更好的用户体验
7. 安全性
npm:
npm audit内置安全审计- npm 8+ 支持
overrides强制版本 - 与 npm registry 集成良好
Yarn:
yarn audit安全审计- 支持
resolutions强制版本 - 更严格的依赖解析
8. 插件和扩展
npm:
- 插件系统相对简单
- 通过 npm scripts 扩展功能
Yarn:
- 丰富的插件生态系统
- 支持 Yarn 2+ 的插件系统
- 更灵活的自定义选项
性能对比
安装速度测试
shell大型项目(1000+ 依赖): - npm 6: ~120s - npm 7+: ~60s - Yarn 1: ~45s - Yarn 2+: ~40s 中型项目(100-500 依赖): - npm 6: ~30s - npm 7+: ~15s - Yarn 1: ~12s - Yarn 2+: ~10s
磁盘空间
shellnode_modules 大小: - npm: 标准嵌套结构 - Yarn 1: 标准嵌套结构 - Yarn 2+ (PnP): 无 node_modules,显著减少空间
选择建议
选择 npm 的场景
- 新项目:npm 7+ 性能已足够好
- 简单项目:不需要复杂的工作区功能
- 团队熟悉:团队已经熟悉 npm
- CI/CD:大多数 CI 环境默认支持 npm
- 发布包:npm 发布流程更简单
选择 Yarn 的场景
- 大型 monorepo:Yarn 工作区更成熟
- 需要离线支持:Yarn 离线模式更好
- 性能敏感:Yarn 通常更快
- 需要 PnP:Yarn 2+ 的 Plug'n'Play 功能
- 团队偏好:团队更喜欢 Yarn 的用户体验
迁移指南
从 npm 迁移到 Yarn
bash# 安装 Yarn npm install -g yarn # 在项目目录运行 yarn install # Yarn 会自动读取 package.json 并生成 yarn.lock
从 Yarn 迁移到 npm
bash# 删除 yarn.lock rm yarn.lock # 运行 npm install npm install # npm 会生成 package-lock.json
高级功能对比
1. 依赖解析策略
npm:
- 嵌套依赖结构
- npm 7+ 使用更智能的解析算法
- 支持依赖提升
Yarn:
- 更严格的依赖解析
- Yarn 2+ 支持零安装(PnP)
- 更好的依赖去重
2. 缓存机制
npm:
bashnpm cache verify npm cache clean --force
Yarn:
bashyarn cache list yarn cache clean
Yarn 的缓存管理更精细。
3. 版本管理
npm:
bashnpm version major npm version minor npm version patch
Yarn:
bashyarn version --major yarn version --minor yarn version --patch
4. 全局包管理
npm:
bashnpm list -g --depth=0 npm uninstall -g <pkg>
Yarn:
bashyarn global list yarn global remove <pkg>
配置文件
.npmrc
ini# npm 配置 registry=https://registry.npmjs.org cache=/path/to/cache strict-ssl=true
.yarnrc.yml
yaml# Yarn 2+ 配置 nodeLinker: node-modules enableGlobalCache: true npmRegistryServer: "https://registry.npmjs.org"
最佳实践
1. 锁文件管理
- 始终提交锁文件到版本控制
- 不要手动编辑锁文件
- 定期更新依赖
2. 版本范围
json{ "dependencies": { // 生产环境使用精确版本 "critical-package": "1.2.3", // 开发环境可以使用范围版本 "dev-package": "^1.2.3" } }
3. 安全审计
bash# npm npm audit npm audit fix # Yarn yarn audit yarn audit --json
4. CI/CD 优化
yaml# GitHub Actions - npm - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' cache: 'npm' # GitHub Actions - Yarn - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' cache: 'yarn'
未来趋势
- npm:持续改进性能和安全性
- Yarn:推动 PnP 和零安装概念
- pnpm:新兴的包管理器,使用硬链接节省空间
- Bun:新一代 JavaScript 运行时,内置包管理器
选择 npm 还是 Yarn 主要取决于项目需求、团队偏好和具体场景。两者都是优秀的包管理器,各有优势。