刚安装npm install xxx时,出现以下如图错误:
原因:因为访问 https://registry.npmjs.org 网址需要翻墙才能访问,所以在国内经常会出现连接超时等问题。为了解决这个问题,可以将npm的源设置为国内的镜像例如淘宝镜像。
以下是设置淘宝镜像的方法:
方法一
在 cmd 管理员方式下执行
//将npm设置为淘宝镜像
npm config set registry http://registry.npmmirror.com
//输入上述命令后,npm就会将镜像源设置为淘宝镜像,可以一下命令来验证即查看npm镜像设置
npm config get registry
//如果输出结果为 https://registry http://registry.npmmirror.com/,则说明已经成功设置镜像源
//尝试重新执行,应该就能够成功安装了
npm install xxx
//另外,如果不想全局设置 npm 的镜像源,也可以在执行 npm install 命令时指定镜像源,
例如:npm install --registry=https://registry.npmmirror.com
如图:
方法二:
在终端cmd
// 执行 nrm ls 查看所有可用的 registry
C:\Users\测试>nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
//执行nrm use taobao 切换到指定的 registry
C:\Users\测试>nrm use taobao
SUCCESS The registry has been changed to 'taobao'.
C:\Users\测试>nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
* taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
然后再次运行 npm install