You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

npm安装超时提示处于代理后,Ubuntu环境下无法安装依赖包

解决npm/npx网络超时问题(Ubuntu 22.04 + Node.js 18.12.1)

1. 清理npm残留代理配置

即使你确认不在代理环境,npm可能残留了无效的代理配置,先检查并清理:

  • 查看当前npm配置:
    npm config list
    
  • 若输出中存在proxy、https-proxy项,执行以下命令删除:
    npm config delete proxy
    npm config delete https-proxy
    

2. 切换npm镜像源

官方npm registry访问不稳定是常见超时原因,切换到国内镜像源可解决:

  • 临时用镜像源安装yarn:
    sudo npm install --global yarn --registry=https://registry.npmmirror.com
    
  • 或永久设置镜像源(后续所有npm操作默认使用):
    npm config set registry https://registry.npmmirror.com
    
    设置完成后再尝试:
    sudo npm install --global yarn
    npx create-react-app my-app
    

3. 修复系统DNS配置

DNS解析异常会导致npm无法连接registry,可更换为公共DNS:

  • 查看当前DNS配置:
    cat /etc/resolv.conf
    
  • 编辑/etc/systemd/resolved.conf,修改DNS配置:
    DNS=8.8.8.8 1.1.1.1
    
  • 重启DNS服务生效:
    sudo systemctl restart systemd-resolved
    

4. 临时禁用IPv6(若网络不支持)

部分网络环境下IPv6解析失败会触发超时,临时禁用测试:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1

若有效,可通过编辑/etc/sysctl.conf添加以下内容永久禁用:

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1

执行sudo sysctl -p使配置生效。

5. 绕开npx,直接用yarn创建React项目

若npx仍失败,安装yarn后直接使用yarn初始化项目:

yarn create react-app my-app

内容的提问来源于stack exchange,提问作者El_Loco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:25:15