npm 安装包遇到问题的常用脚本(RequestError: socket hang up)

前言

最近在给一个基于 Electron 的开源项目做贡献,需要去安装一些 npm 库,由于众所周知的原因,经常会出现报错:

复制代码
npm ERR! path D:\Projects\project\node_modules\electron
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node install.js
npm ERR! RequestError: socket hang up
npm ERR!     at ClientRequest.<anonymous> (D:\Projects\project\node_modules\@electron\get\node_modules\got\dist\source\core\index.js:970:111)
npm ERR!     at Object.onceWrapper (node:events:632:26)
npm ERR!     at ClientRequest.emit (node:events:529:35)
npm ERR!     at origin.emit (D:\Projects\project\node_modules\@szmarczak\http-timer\dist\source\index.js:43:20)
npm ERR!     at TLSSocket.socketOnEnd (node:_http_client:525:9)
npm ERR!     at TLSSocket.emit (node:events:529:35)
npm ERR!     at endReadableNT (node:internal/streams/readable:1368:12)
npm ERR!     at connResetException (node:internal/errors:720:14)
npm ERR!     at TLSSocket.socketOnEnd (node:_http_client:525:23)
npm ERR!     at TLSSocket.emit (node:events:529:35)
npm ERR!     at endReadableNT (node:internal/streams/readable:1368:12)
npm ERR!     at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

npm ERR! A complete log of this run can be found in: C:\Users\xxx\AppData\Local\npm-cache\_logs\2023-12-17T04_00_17_970Z-debug-0.log

这是因为网络连接的问题而无法安装,尝试了国内镜像但是也会出一些奇怪的问题。下面假设你有一个合规的代理。

正文

复制代码
npm config set proxy=http://127.0.0.1:7891
npm config set https-proxy=http://127.0.0.1:7891
npm install vue  # 安装需要的包

但是有些情况下还是会遇到神奇的问题。可以尝试下面的命令更新 npm 解决。

bash 复制代码
npm install npm -

PS:从参考来源那里拷贝的代码在命令尾部包括了一个`-`。我在测试中使用这个带 `-` 的命令,并且后面顺利安装了。但是目前并不明确这个 `-` 的意义,简单浏览了官方文档没有看到类似的写法。(LLM表示这个可能就是表示这个命令结束用的。也有LLM说这个代表 @latest,但是同样没有在文档中找到相关描述,不排除幻觉可能,但是这个事情不重要,蹲大神赐教了)

Reference

解决各种 npm install 报错问题_coc.nvim 配置 error getaddrinfo eai_again re-CSDN博客

相关推荐
晓杰'5 小时前
Balatro后端进阶(2):基于GitHub Actions的CI自动化验证实现
websocket·ci/cd·typescript·node.js·自动化·github·nestjs
弹简特8 小时前
【Vue3速成】02-vue工程化目录结构+执行原理
前端·vue.js·npm
008爬虫实战录1 天前
【码上爬】 题十二:如来神掌 困难, JSVMP加密,使用代理补环境
前端·javascript·node.js
弹简特1 天前
【Vue3速成】01-npm+vue初体验+vite构建vue工程化
vue.js·arcgis·npm
DogDaoDao1 天前
Windows 下 Git 报错:`touch` 无法识别 —— 原因分析与 7 种解决方案(从入门到精通)
windows·git·程序员·npm·powershell·cmd·touch
008爬虫实战录1 天前
【码上爬】 题九:webpack调试 堆栈分析
前端·webpack·node.js
xiaoxue..1 天前
Node.js 笔试题讲解
后端·面试·node.js
小小前端仔LC1 天前
Node.js + LangChain +React:搭建个人知识库(四)- 把向量和文件切块存入mysql中
后端·node.js
海上彼尚1 天前
Nodejs也能写Agent - 7.基础篇 - MCP
前端·javascript·人工智能·node.js
天若有情6731 天前
轻量级状态事件总线 eventbusx-js 开源使用教程
开发语言·javascript·npm·开源·事件·事件总线