npm 和 yarn 更换镜像源完整指南
目录
在国内使用 npm
或 yarn
安装依赖时,常遇到下载缓慢或失败的问题。通过配置国内镜像源,可显著提升安装速度和成功率。
一、npm 镜像源配置
1. 查看当前镜像源
npm config get registry
2. 设置国内镜像源
淘宝镜像源:
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/
3. 镜像源速度测试脚本(可选)
#!/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
4. 恢复官方默认源
npm config set registry https://registry.npmjs.org
5. 临时使用镜像源(不修改全局配置)
npm install --registry https://registry.npmmirror.com
npm install lodash --registry https://registry.npmmirror.com
6. 为特定包设置镜像(如 Electron、node-sass 等)
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
7. 查看与编辑配置
npm config list # 查看所有配置
npm config edit # 编辑配置文件
二、yarn 镜像源配置
1. 查看当前镜像源
yarn config get registry
2. 设置淘宝镜像
yarn config set registry https://registry.npmmirror.com
yarn config get registry
3. 其他国内镜像源
yarn config set registry https://mirrors.huaweicloud.com/repository/npm/
yarn config set registry https://mirrors.cloud.tencent.com/npm/
4. 恢复默认源
yarn config set registry https://registry.yarnpkg.com
5. 临时指定镜像源
yarn install --registry https://registry.npmmirror.com
yarn add lodash --registry https://registry.npmmirror.com
6. 配置其他项
yarn config list
yarn config delete registry
yarn config set sass_binary_site https://npmmirror.com/mirrors/node-sass
三、使用 nrm 快速切换镜像源(推荐)
nrm
是 npm 镜像源管理工具,支持快速切换源并测试速度。
1. 安装 nrm
npm install -g nrm
2. 常用命令
nrm ls # 查看所有镜像源
nrm use taobao # 切换到淘宝源
nrm use npm # 切换回官方源
nrm test # 测试各镜像源速度
nrm add custom https://your-registry.com/ # 添加自定义源
nrm del custom # 删除自定义源
四、NVM 用户的镜像配置
对于使用 nvm 管理 Node.js 的用户,可以设置环境变量加速 Node 下载:
在 .bashrc
/ .zshrc
等配置文件中添加:
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
七、Docker / CI 环境配置
1. Dockerfile 中配置
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
2. GitHub Actions 配置
- 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/
相关文章
阿里云百炼大模型
9折优惠 + 所有模型各百万免费Token →