【npm】设置国内镜像源
为了提高 npm 的安装速度,你可以将 npm 的镜像源设置为国内的镜像源。以下是常用的国内镜像源及其设置方法:
1. 淘宝 NPM 镜像
淘宝提供了国内最常用的 npm 镜像源,地址为:https://registry.npmmirror.com
设置淘宝镜像源
运行以下命令将 npm 源设置为淘宝镜像:
npm config set registry https://registry.npmmirror.com
恢复官方源
如果需要恢复官方源,可以运行:
npm config set registry https://registry.npmjs.org/
2. 华为云 NPM 镜像
华为云也提供了 npm 镜像源,地址为:https://repo.huaweicloud.com/repository/npm/
设置华为云镜像源
运行以下命令:
npm config set registry https://repo.huaweicloud.com/repository/npm/
3. 腾讯云 NPM 镜像
腾讯云提供了 npm 镜像源,地址为:https://mirrors.cloud.tencent.com/npm/
设置腾讯云镜像源
运行以下命令:
npm config set registry https://mirrors.cloud.tencent.com/npm/
4. 查看当前镜像源
你可以通过以下命令查看当前设置的镜像源:
npm config get registry
5. 使用 nrm
管理镜像源
nrm
是一个 npm 源管理工具,可以方便地切换不同的镜像源。
安装 nrm
运行以下命令安装 nrm
:
npm install -g nrm
查看可用镜像源
运行以下命令查看支持的镜像源列表:
nrm ls
输出示例:
* npm ---- https://registry.npmjs.org/
yarn ---- https://registry.yarnpkg.com/
tencent - https://mirrors.cloud.tencent.com/npm/
taobao -- https://registry.npmmirror.com/
huawei -- https://repo.huaweicloud.com/repository/npm/
切换镜像源
运行以下命令切换到淘宝镜像源:
nrm use taobao
测试镜像源速度
运行以下命令测试各个镜像源的速度:
nrm test
总结
-
推荐使用淘宝镜像源:
https://registry.npmmirror.com
-
可以使用
nrm
工具方便地管理和切换镜像源。 -
如果需要恢复官方源,运行
npm config set registry https://registry.npmjs.org/
。
设置国内镜像源后,npm 的安装速度会显著提升!