配置npm国内源的方法
通过修改npm的registry配置为国内镜像源,可以显著提升依赖包的下载速度。以下为详细步骤和可选镜像列表:
临时使用单个镜像源
在安装特定包时,可通过--registry
参数临时指定镜像源:
npm install [package-name] --registry=https://registry.npmmirror.com
永久修改默认镜像源
执行以下命令永久更换registry:
npm config set registry https://registry.npmmirror.com
验证配置是否生效:
npm config get registry
国内主流npm镜像源列表
-
阿里云镜像
https://registry.npmmirror.com
-
腾讯云镜像
https://mirrors.cloud.tencent.com/npm/
-
华为云镜像
https://repo.huaweicloud.com/repository/npm/
-
清华大学镜像
https://mirrors.tuna.tsinghua.edu.cn/npm/
-
cnpmjs镜像
https://r.cnpmjs.org/
使用nrm快速切换源
安装nrm源管理工具:
npm install -g nrm
查看可用源列表:
nrm ls
切换镜像源(以阿里云为例):
nrm use taobao
测试各源延迟:
nrm test
恢复默认官方源
如需切换回官方源:
npm config set registry https://registry.npmjs.org
注意事项
-
企业私有源需配置认证信息:
npm config set always-auth true npm config set _auth [base64-encoded-auth-token]
-
镜像源同步可能存在延迟,重要生产依赖建议测试后使用
-
部分镜像源(如cnpm)可能修改包内容,金融类项目慎用