npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......

问题阐述

最近在本地部署一个前后端分离项目的时候遇到了这个问题,npm命令无法使用总是显示网络连接异常,由于官方源在国外,我于是配置国内淘宝镜像来提升下载速度:

#设置淘宝镜像源
npm config set registry https://registry.npmmirror.com
#检查是否设置成功
npm get registry  

同时也将yarn设置了淘宝镜像:

#设置淘宝镜像源
yarn config set registry https://registry.npmmirror.com
#检查是否设置成功
yarn config get registry   

当然也可以下载cgr镜像源管理工具(前提是你能用npm下载)来方便管理:

  1. 安装 cgr
npm install -g cgr
  1. 列出当前可用的镜像源
cgr ls
   npm ---- https://registry.npmjs.org/
   cnpm --- http://r.cnpmjs.org/
   taobao - https://registry.npmmirror.com/
   yarn --- https://registry.yarnpkg.com/
  1. 选择一个镜像源进行切换(淘宝)
cgr use taobao
   npm registry has been set to: https://registry.npmmirror.com/
   yarn registry has been set to: https://registry.npmmirror.com/
  1. 测试访问速度
cgr test taobao

但是当我换源之后遇到了以下问题:
npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第1张图片
npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第2张图片
两个包管理工具下载都报类似的错误:ERR!network reguest to https://registry.npumirror. com/yarn failed, reason: connect ETIMEDOUT 240c:c0a9:1600:1:3::3ie:443

解决办法

可能是ipv6网络连接不稳定,导致下载连接异常。最后的解决办法是将本地WLAN属性的ipv6关掉,这样才能下载,当然镜像源还是得设置为淘宝源。

打开本地控制面板,并点击网络和 Internet

npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第3张图片
npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第4张图片

点击网络和共享中心,再点击更改适配器设置

npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第5张图片
npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第6张图片

右键点击WLAN,再点击属性

npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第7张图片

取消ipv6,不勾选

npm和yarn换成淘宝源仍报错npm ERR! code ETIMEDOUT和info There appears to be trouble with your network......_第8张图片

总结

至此npm和yarn便能正常使用,问题得到解决,其实最开始我将npm的镜像源设置为华为源https://mirrors.huaweicloud.com/repository/npm/,ipv6也没有关闭,yarn也设置为华为源,但是这样有一个问题就是npm能用而yarn用不了,直到关闭ipv6,两个源都改成淘宝源才能正常下载依赖包,用npm和yarn下载依赖包的问题困扰了我6个多小时,故有感而发写下这篇文章,留给那些遇到类似问题而未能解决的coder,节省开发时间。

你可能感兴趣的:(前端,npm,前端,node.js)