Linux环境下npm/yarn网络连接异常问题求助
问题:Linux下npm/yarn频繁报网络超时,pnpm可正常使用
- 两台Linux设备(台式机+笔记本)均出现此问题
- 浏览器网络访问正常,但npm、yarn执行命令时频繁触发网络连接错误,pnpm可正常工作
- 已尝试方案:重启网络设备、延长
--network-timeout、清理npm/yarn缓存、更新软件版本,均未解决问题
报错详情
Yarn报错:
info There appears to be trouble with your network connection. Retrying... error AggregateError at internalConnectMultiple (node:net:1114:18) at internalConnectMultiple (node:net:1177:5) at Timeout.internalConnectMultipleTimeout (node:net:1687:3) at listOnTimeout (node:internal/timers:575:11) at process.processTimers (node:internal/timers:514:7)
NPM报错:
npm ERR! code ETIMEDOUT npm ERR! errno ETIMEDOUT npm ERR! network request to https://registry.npmjs.org/@types%2fcookie-session failed, reason: npm ERR! network This is a problem related to network connectivity. npm ERR! network In most cases you are behind a proxy or have bad network settings. npm ERR! network npm ERR! network If you are behind a proxy, please make sure that the npm ERR! network 'proxy' config is set properly. See: 'npm help config' npm ERR! A complete log of this run can be found in: /home/liport/.npm/_logs/2024-01-09T05_28_45_333Z-debug-0.log
解决方案
1. 重置npm/yarn代理配置
pnpm可能自动忽略了无效代理,而npm/yarn仍沿用错误配置,执行以下操作:
- 查看npm代理:
npm config get proxy、npm config get https-proxy - 清空npm代理:
npm config delete proxy、npm config delete https-proxy - 查看yarn代理:
yarn config get proxy、yarn config get https-proxy - 清空yarn代理:
yarn config delete proxy、yarn config delete https-proxy
2. 切换国内镜像源
默认npmjs.org在部分网络环境下访问不稳定,切换到国内镜像:
- npm切换:
npm config set registry https://registry.npmmirror.com - yarn切换:
yarn config set registry https://registry.npmmirror.com
3. 清理系统级代理环境变量
检查全局代理变量是否干扰npm/yarn:
echo $HTTP_PROXY echo $HTTPS_PROXY echo $http_proxy echo $https_proxy
若有输出,临时清空:
unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy
永久生效需修改~/.bashrc或~/.zshrc,删除相关export语句,再执行source ~/.bashrc(对应shell配置文件)
4. 检查防火墙/安全组规则
临时关闭防火墙测试是否被拦截:
- Ubuntu/Debian:
sudo ufw disable - CentOS/RHEL:
sudo systemctl stop firewalld
测试正常后,需添加npm/yarn的网络访问规则,再重新开启防火墙。
5. 强制使用IPv4
部分环境下IPv6解析异常导致超时,强制启用IPv4:
- npm:
npm config set registry http://registry.npmmirror.com --ipv4,或执行命令时加参数npm install --ipv4 - yarn:设置环境变量
export YARN_NODE_OPTIONS="--dns-result-order=ipv4first",再执行yarn命令
内容的提问来源于stack exchange,提问作者Robiul
相关产品推荐
相关产品推荐

