执行npm install报错无法连接到github.com的问题

本文详细解析了在Docker环境下构建VueNode项目时遇到的npm install连接GitHub失败的问题,提供了修改git协议从git改为https的解决方案,并进一步解决了因sass源问题导致的构建错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错信息如下:

Step 6/11 : RUN npm install
 ---> Running in fbea7757df0b
npm WARN ub-arc-mgmt@4.2.1 license should be a valid SPDX license expression

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t git://github.com/adobe-webplatform/eve.git
npm ERR! 
npm ERR! fatal: unable to connect to github.com:
npm ERR! github.com[0: 52.74.223.119]: errno=Connection timed out
npm ERR! 
npm ERR! 
npm ERR! exited with error code: 128

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-02-27T09_03_17_432Z-debug.log

 

场景:

在用docker 构建vue node项目镜像时,执行npm install 命令是包连接不上github.com

网上搜索到解决方案:

参考:

https://www.daxiblog.com/npm-install%e8%a7%a3%e5%86%b3fatal-unable-to-connect-to-github-com%e9%97%ae%e9%a2%98/

引用上文的解释:

意思是我连接不上github。但是不可能啊,我能够访问github的官网。而且在其他工程里执行npm install都能很顺利执行完成了。看来很有可能是上面截图中eve.git这个库的引用防止有些不太一致造成的。于是在通过在网络上搜寻,找到了原因所在:

上图中可以看到这个eve.git这个库的引用是通过git写引用的。git协议的默认端口是9418,很有可能是我的网络环境防火墙并没有开放这个端口造成的。解决方法就是,用https替代git协议。配置的命令为:

 git config --global url."https://".insteadOf git://

docker 中解决方案:

在Dockerfile中执行npm install之前,执行上述命令

修改如下:

//解决npn install报错连接不上git的问题。
RUN git config --global url."https://".insteadOf git://
RUN npm install

 

后续npm install问题接龙:

1、npm ERR! Failed at the node-sass@4.13.0 postinstall script.

gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/home/erms/uicode/ub-arc-mgmt-gui/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:210:5)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
gyp ERR! System Linux 3.10.0-693.el7.x86_64
gyp ERR! command "/root/.nvm/versions/node/v12.13.1/bin/node" "/home/erms/uicode/ub-arc-mgmt-gui/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /home/erms/uicode/ub-arc-mgmt-gui/node_modules/node-sass
gyp ERR! node -v v12.13.1
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
Build failed with error code: 1
npm WARN ub-arc-mgmt@4.2.1 license should be a valid SPDX license expression
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.13.0 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node-sass@4.13.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-03-01T12_04_15_682Z-debug.log

解决:

这个是因为sass安装时获取源的问题,先修改sass安装的源,再运行npm install就成功了

npm config set sass_binary_site=https://npm.taobao.org/mirrors/node-sass

 

### npm install 错误 4501 的解决方案 当遇到 `npm install` 过程中的错误 4501 时,通常这可能是由于依赖包之间的不兼容或者本地环境配置问题引起的。针对此类问题可以采取以下措施来尝试解决问题。 #### 清理缓存并重试安装 有时 NPM 缓存可能损坏或过期,清理缓存有助于消除潜在冲突。 ```bash npm cache clean --force ``` #### 更新 Node.js 和 NPM 版本 确保使用的 Node.js 及其配套工具链处于最新稳定版能够减少许多不必要的麻烦[^2]。 #### 使用淘宝镜像源加速下载速度 对于国内开发者来说,切换到更快捷的注册表服务如 Taobao Registry 能显著提升效率。 ```bash npm config set registry https://registry.npmmirror.com/ ``` #### 配置合适的 Python 环境变量 (仅限 Windows 用户) 如果项目涉及到编译原生模块,则需确认已正确设置了 Python PATH 并选择了适当版本作为构建工具的一部分。 #### 手动指定特定版本号进行安装 考虑到可能存在某些库之间相互依赖关系复杂的情况,建议按照官方文档推荐的方式精确指明所需组件及其对应版本来进行安装操作。例如处理 Sass 相关插件时可参照如下命令执行: ```bash npm uninstall node-sass sass-loader npm install node-sass@6.0.1 sass-loader@7.0.1 ``` 以上方法综合运用往往能有效应对大部分常规性的 `npm install` 失败情形。当然具体情况还需具体分析,必要时候查阅目标项目的 GitHub Issues 页面获取更多针对性帮助也是不错的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值