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博客

相关推荐
SEO_juper2 小时前
HTTPS 混合内容检查工具:页面全站 https 了,但还有 http 资源在拖后腿
开发语言·网络协议·http·https·node.js·json·bash
晓得迷路了1 天前
栗子前端技术周刊第 156 期 - jQuery 4.1、TanStack Query v6 路线图...
前端·javascript·node.js
YIAN2 天前
流式 TTS 和普通TTS到底有什么区别?一个文档教会你!
前端·websocket·node.js
光影少年3 天前
ts全栈开发
javascript·后端·typescript·node.js·c5全栈
烟雨空灵6 天前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
用户216076340836 天前
1200 个任务里,97 个还开着,0 个我能做
node.js
不要试图纠正别人6 天前
1055 个文件,共享 0 字节:pnpm 硬链接的静默降级
前端·node.js
吴名氏.7 天前
从零手写一个 AI Agent 框架(TypeScript 完整实现):Function Calling 循环、流式输出与错误恢复
人工智能·typescript·node.js·大模型·ai agent·functioncalling·llm 应用开发
关键词安全8 天前
PHP / Node.js / Go / Rust 全部中招(CVE-2026-109618、109619、111497、111943)完整排查与修复清单
golang·rust·node.js
zhensherlock9 天前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js