乐闻世界logo
搜索文章和话题

npm 缓存机制是怎样的?如何优化依赖安装速度?

2月17日 23:23

npm 提供了多种缓存机制来加速包的安装和减少网络请求。理解缓存机制对于优化构建性能和解决安装问题非常重要。

npm 缓存位置

默认缓存位置

npm 缓存默认存储在以下位置:

  • macOS/Linux: ~/.npm
  • Windows: %AppData%/npm-cache

查看缓存位置

bash
npm config get cache

缓存目录结构

shell
~/.npm/ ├── _cacache/ │ ├── content-v2/ │ │ └── sha512/ │ │ └── ... │ └── index-v5/ │ └── ... └── _logs/

缓存管理命令

查看缓存信息

bash
# 查看缓存大小 npm cache verify # 查看缓存目录 npm config get cache

清理缓存

bash
# 清理缓存(npm 5+) npm cache clean --force # 清理特定包的缓存 npm cache clean <package-name>

验证缓存

bash
# 验证缓存完整性 npm cache verify

缓存工作原理

1. 包下载缓存

当安装包时,npm 会:

  1. 检查本地缓存是否存在该包
  2. 如果存在且未过期,直接使用缓存
  3. 如果不存在或已过期,从 registry 下载
  4. 将下载的包存储到缓存

2. 缓存键

缓存使用内容的 SHA-512 哈希作为键:

shell
~/.npm/_cacache/content-v2/sha512/<hash>

3. 缓存索引

索引文件存储包元数据和位置信息:

shell
~/.npm/_cacache/index-v5/

缓存配置

配置缓存大小

bash
# 设置缓存大小限制(默认为 10GB) npm config set cache-max 10737418240 # 设置缓存最小保留时间(秒) npm config set cache-min 3600

配置缓存策略

bash
# 禁用缓存(不推荐) npm config set cache false # 设置缓存目录 npm config set cache /path/to/cache

离线安装

使用缓存进行离线安装:

bash
# 使用缓存安装(不检查网络) npm install --offline # 使用缓存但允许网络回退 npm install --prefer-offline # 优先使用网络,缓存作为后备 npm install --prefer-online

CI/CD 中的缓存优化

GitHub Actions 示例

yaml
name: Build and Test on: [push, pull_request] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' cache: 'npm' - name: Install dependencies run: npm ci - name: Run tests run: npm test

Docker 优化

dockerfile
FROM node:18-alpine # 复制 package 文件 COPY package*.json ./ # 安装依赖(利用缓存) RUN npm ci --only=production # 复制应用代码 COPY . . CMD ["npm", "start"]

自定义缓存目录

bash
# 设置自定义缓存目录 export npm_config_cache=/custom/cache/path npm install

缓存相关问题和解决方案

1. 缓存损坏

症状:安装失败或出现奇怪的错误

解决方案

bash
npm cache clean --force npm install

2. 缓存占用过多空间

解决方案

bash
# 查看缓存大小 npm cache verify # 清理缓存 npm cache clean --force # 定期清理(添加到 crontab) 0 0 * * 0 npm cache clean --force

3. 缓存导致版本不一致

解决方案

bash
# 强制重新下载 npm install --force # 删除 node_modules 和 package-lock.json rm -rf node_modules package-lock.json npm install

性能优化技巧

1. 使用 npm ci

npm ci 专门用于 CI 环境,比 npm install 更快:

bash
npm ci

优势

  • 直接使用 package-lock.json
  • 跳过某些用户级配置
  • 删除 node_modules 后重新安装
  • 更快的安装速度

2. 并行安装

npm 7+ 支持并行安装:

bash
# 设置并行安装数量 npm config set maxsockets 50

3. 使用镜像

使用国内镜像加速下载:

bash
# 使用淘宝镜像 npm config set registry https://registry.npmmirror.com # 使用 cnpm npm install -g cnpm --registry=https://registry.npmmirror.com

4. 预安装常用包

bash
# 全局安装常用包 npm install -g nodemon typescript # 使用 npm link 共享本地包 npm link ../local-package

缓存监控

查看缓存统计

bash
# 查看缓存大小 du -sh ~/.npm # 查看缓存目录结构 tree ~/.npm/_cacache -L 2

缓存分析工具

bash
# 使用 npm-cache-stats npm install -g npm-cache-stats npm-cache-stats

最佳实践

1. 开发环境

  • 保持默认缓存配置
  • 定期清理缓存(每月一次)
  • 使用 npm cache verify 检查缓存完整性

2. CI/CD 环境

  • 使用 npm ci 替代 npm install
  • 配置缓存层(GitHub Actions cache、Docker volume)
  • 设置合理的缓存保留策略

3. 生产环境

  • 使用 npm ci --production 只安装生产依赖
  • 考虑使用 .npmrc 锁定配置
  • 定期清理缓存以释放空间

4. 离线环境

  • 预先下载所有依赖到缓存
  • 使用 npm install --offline 进行离线安装
  • 考虑使用私有 registry

.npmrc 配置示例

ini
# 项目级 .npmrc cache=/path/to/project/.npm-cache registry=https://registry.npmmirror.com prefer-offline=true fetch-retries=3 fetch-retry-mintimeout=20000 fetch-retry-maxtimeout=120000

常见问题排查

安装速度慢

bash
# 检查网络连接 ping registry.npmjs.org # 使用镜像 npm config set registry https://registry.npmmirror.com # 清理缓存 npm cache clean --force

缓存问题导致安装失败

bash
# 清理缓存 npm cache clean --force # 删除 node_modules 和 lock 文件 rm -rf node_modules package-lock.json # 重新安装 npm install

磁盘空间不足

bash
# 查看缓存大小 du -sh ~/.npm # 清理缓存 npm cache clean --force # 设置缓存大小限制 npm config set cache-max 5368709120

理解 npm 缓存机制可以帮助开发者优化构建性能、解决安装问题,并在 CI/CD 环境中提高效率。

标签:NPM