在 npm 中设置镜像源可以显著提升依赖包的下载速度,尤其是在国内访问官方源 (https://registry.npmjs.org
) 较慢时。以下是 npm 镜像源设置方法和国内常用稳定镜像源推荐。
1. npm 设置镜像源的方法
1.1. 临时使用镜像源
在安装包时通过 --registry
参数临时指定镜像源:
1
| npm install [包名] --registry=https://registry.npmmirror.com
|
例如:
1
| npm install -g hexo-cli --registry=https://registry.npmmirror.com
|
1.2. 永久切换镜像源
1.2.1. 使用 npm config set
命令
1
| npm config set registry https://registry.npmmirror.com
|
1.2.2. 直接修改 npm 配置文件
编辑 ~/.npmrc
(Linux/macOS)或 C:\Users\<用户名>\.npmrc
(Windows),添加:
1
| registry=https://registry.npmmirror.com
|
1.3. 查看当前镜像源
1.4. 恢复默认官方源
1
| npm config set registry https://registry.npmjs.org
|
2. 国内常用稳定镜像源
镜像源名称 |
地址 |
维护方 |
备注 |
淘宝 npm 镜像 |
https://registry.npmmirror.com |
阿里巴巴 |
国内最稳定,同步频率高(10分钟一次) |
华为云 npm 镜像 |
https://mirrors.huaweicloud.com/repository/npm/ |
华为云 |
适合华为云用户 |
腾讯云 npm 镜像 |
https://mirrors.cloud.tencent.com/npm/ |
腾讯云 |
适合腾讯云用户 |
cnpm 镜像 |
https://r.cnpmjs.org/ |
cnpm 团队 |
早期常用,但不如淘宝镜像稳定 |
npm 官方源 |
https://registry.npmjs.org |
npm Inc. |
国外源,国内访问可能较慢 |
推荐使用淘宝镜像 (https://registry.npmmirror.com
),因为它:
- 同步频率高(10分钟一次)
- 国内访问速度快
- 稳定性好