npm 和 yarn 更换镜像源完整指南

在国内使用 npmyarn 安装依赖时,常遇到下载缓慢或失败的问题。通过配置国内镜像源,可显著提升安装速度和成功率。


npm config get registry

淘宝镜像源:

npm config set registry https://registry.npmmirror.com
npm config get registry  # 验证设置是否成功

其他镜像源:

# 华为云
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

# 腾讯云
npm config set registry https://mirrors.cloud.tencent.com/npm/

# 中科大
npm config set registry https://npmreg.proxy.ustclug.org/
#!/bin/bash
# test-registry.sh

registries=(
  "https://registry.npmjs.org"
  "https://registry.npmmirror.com"
  "https://mirrors.huaweicloud.com/repository/npm/"
  "https://mirrors.cloud.tencent.com/npm/"
)

for registry in "${registries[@]}"; do
  echo "Testing $registry"
  time npm install --registry="$registry" lodash --no-save --silent
  npm uninstall lodash --silent
  echo "---"
done
npm config set registry https://registry.npmjs.org
npm install --registry https://registry.npmmirror.com
npm install lodash --registry https://registry.npmmirror.com
npm config set electron_mirror=https://npmmirror.com/mirrors/electron/
npm config set sass_binary_site=https://npmmirror.com/mirrors/node-sass
npm config set puppeteer_download_host=https://npmmirror.com/mirrors
npm config list     # 查看所有配置
npm config edit     # 编辑配置文件

yarn config get registry
yarn config set registry https://registry.npmmirror.com
yarn config get registry
yarn config set registry https://mirrors.huaweicloud.com/repository/npm/
yarn config set registry https://mirrors.cloud.tencent.com/npm/
yarn config set registry https://registry.yarnpkg.com
yarn install --registry https://registry.npmmirror.com
yarn add lodash --registry https://registry.npmmirror.com
yarn config list
yarn config delete registry
yarn config set sass_binary_site https://npmmirror.com/mirrors/node-sass

nrm 是 npm 镜像源管理工具,支持快速切换源并测试速度。

npm install -g nrm
nrm ls                # 查看所有镜像源
nrm use taobao        # 切换到淘宝源
nrm use npm           # 切换回官方源
nrm test              # 测试各镜像源速度
nrm add custom https://your-registry.com/  # 添加自定义源
nrm del custom                             # 删除自定义源

对于使用 nvm 管理 Node.js 的用户,可以设置环境变量加速 Node 下载:

export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs/

npm config get registry
yarn config get registry

# 测试安装速度
time npm install lodash
time yarn add lodash

# 查看下载地址
npm view lodash dist.tarball

# 清除缓存
npm cache clean --force
yarn cache clean

# 删除并重新安装依赖
rm -rf node_modules package-lock.json yarn.lock
npm install
# 或
yarn install

FROM node:18-alpine

RUN npm config set registry https://registry.npmmirror.com
# 或者使用环境变量
ENV NPM_CONFIG_REGISTRY=https://registry.npmmirror.com

COPY package*.json ./
RUN npm install
- name: Setup Node.js
  uses: actions/setup-node@v3
  with:
    node-version: '18'
    registry-url: 'https://registry.npmmirror.com'

# 设置公司私有源
npm config set registry http://your-internal-registry.com/

# 设置认证信息
npm config set //your-internal-registry.com/:_authToken your-token

# 设置作用域包
npm config set @yourcompany:registry http://your-internal-registry.com/