electron打包ffi-napi报错 npm ERR! gyp reason: read ECONNRESET

本文介绍了一个关于electron+vue项目中使用ffi-napi依赖包时遇到的打包错误问题及解决方法。错误主要表现为网络连接失败导致的fetch error,通过更换网络连接方式成功解决了该问题。

问题描述

这个问题用了我两天的时间,所以记录一下。

我们项目是使用electron+vue,做支付功能的时候需要使用到ffi-napi依赖包。
最后打包的时候ffi-napi报错了,在package.json中去掉ffi-napi就可以打包,但是打包运行后提示缺少ffi-napi,所以必须带着ffi-napi打包。

打包的时候报下面的错误
在这里插入图片描述

 OKAY  take it away `electron-builder`

  • electron-builder version=20.44.4
  • loaded configuration file=package.json ("build" field)
  • writing effective config file=build\builder-effective-config.yaml
  • rebuilding native production dependencies platform=win32 arch=ia32
Error: C:\Program Files (x86)\nodejs\node.exe exited with code 1
Error output:
npm ERR! code 1
npm ERR! path D:\Project\zyd-windows\node_modules\ffi-napi
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp-build
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.4.1
npm ERR! gyp info using node@14.17.0 | win32 | ia32
npm ERR! gyp info find Python using Python version 3.8.5 found at "C:\Users\yaoqi\AppData\Local\Programs\Python\Python38\python.exe"
npm ERR! gyp http GET https://atom.io/download/electron/v9.4.4/node-v9.4.4-headers.tar.gz
npm ERR! gyp WARN install got an error, rolling back install
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack FetchError: request to https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist/v9.4.4/node-v9.4.4-headers.tar.gz failed, reason: read ECONNRESET
npm ERR! gyp ERR! stack     at ClientRequest.<anonymous> (D:\Project\zyd-windows\node_modules\minipass-fetch\lib\index.js:110:14)
npm ERR! gyp ERR! stack     at ClientRequest.emit (events.js:376:20)
npm ERR! gyp ERR! stack     at TLSSocket.socketErrorListener (_http_client.js:475:9)
npm ERR! gyp ERR! stack     at TLSSocket.emit (events.js:388:22)
npm ERR! gyp ERR! stack     at emitErrorNT (internal/streams/destroy.js:106:8)
npm ERR! gyp ERR! stack     at emitErrorCloseNT (internal/streams/destroy.js:74:3)
npm ERR! gyp ERR! stack     at processTicksAndRejections (internal/process/task_queues.js:82:21)
npm ERR! gyp ERR! System Windows_NT 10.0.22000
npm ERR! gyp ERR! command "C:\\Program Files (x86)\\nodejs\\node.exe" "D:\\Project\\zyd-windows\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd D:\Project\zyd-windows\node_modules\ffi-napi
npm ERR! gyp ERR! node -v v14.17.0
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok

核心问题

上面的错误主要是
npm ERR! gyp ERR! stack FetchError: request to https://gh-contractor-zcbenz.s3.amazonaws.com/atom-shell/dist/v9.4.4/node-v9.4.4-headers.tar.gz failed, reason: read ECONNRESET

解决过程

刚开始我以为是ffi-napi依赖包的问题,然后重新npm install了好多次,打包依然是这个问题。
然后百度有人说是镜像源的问题,换了镜像源还是不行。

最后仔细看了报错里面最后原因是read ECONNRESET,网上其他人报错都是403,我的是根本没连接上服务器。然后我就怀疑是网络的问题。因为我连的是公司的网络,公司的网络经常会屏蔽一些东西,比如哔哩哔哩就打不开。

解决方法

我本来连的是公司的wifi,更换了一下网络,打开手机热点,让电脑连接手机热点。
然后就打包成功了!
在这里插入图片描述

npm ERR! code 1 npm ERR! path D:\dpproject\dpkj\dpkj-kxd\node_modules\ffi-napi npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp-build npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@8.4.1 npm ERR! gyp info using node@18.19.1 | win32 | x64 npm ERR! gyp info find Python using Python version 3.9.7 found at "E:\anaconda\python.exe" npm ERR! gyp ERR! find VS npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt npm ERR! gyp ERR! find VS checking VS2022 (17.4.33213.308) found at: npm ERR! gyp ERR! find VS "E:\Visual Studio\2022\Community" npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features" npm ERR! gyp ERR! find VS - found VC++ toolset: v143 npm ERR! gyp ERR! find VS - missing any Windows SDK npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use npm ERR! gyp ERR! find VS looking for Visual Studio 2015 npm ERR! gyp ERR! find VS - not found npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8 npm ERR! gyp ERR! find VS npm ERR! gyp ERR! find VS ************************************************************** npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload. npm ERR! gyp ERR! find VS For more information consult the documentation at: npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows npm ERR! gyp ERR! find VS ************************************************************** npm ERR! gyp ERR! find VS npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use npm ERR! gyp ERR! stack at VisualStudioFinder.fail (D:\dpproject\dpkj\dpkj-kxd\node_modules\node-gyp\lib\find-visualstudio.js:122:47) npm ERR! gyp ERR! stack
最新发布
09-03
<think> 我们正在解决npm安装ffi-napi时出现的gyp找不到Visual Studio安装的错误。根据引用内容,我们可以总结出以下关键点: 1. 引用[3]提到需要ffi正常工作的核心python和c++编译环境。 2. 引用[4]详细说明了在Windows系统下安装编译工具链的方法:使用管理员模式的cmd运行`npm install --global windows-build-tools`命令来安装Python环境和.NET环境,并且强调要用npm安装(不要用cnpm),并且要等待流程完全跑完,之后可能需要配置环境变量。 因此,解决此问题的核心步骤是安装必要的编译工具(windows-build-tools),因为它会安装构建所需的Visual C++构建工具和Python。 具体步骤: 1. 以管理员身份打开命令提示符(cmd)或PowerShell。 2. 运行以下命令安装windows-build-tools: ``` npm install --global windows-build-tools ``` 注意:安装过程可能会花费一些时间,请耐心等待直到完成。过程中可能会停滞在某些步骤(如下载),但不要中断它。 3. 安装完成后,关闭当前命令行窗口,重新打开一个新的管理员命令行窗口(以确保环境变量生效),然后尝试重新安装ffi-napi。 如果上述步骤后仍然出现错误,可能需要手动配置环境变量,将windows-build-tools安装的Python路径添加到系统环境变量PATH中。默认安装路径通常为: - Python: `%USERPROFILE%\.windows-build-tools\python27\` 或类似路径(具体版本可能不同) - 同时,确保Visual Studio构建工具的环境变量已设置(windows-build-tools应该已经处理了这部分)。 另外,引用[1]提到如果是网络问题(例如无法访问某些资源),可能需要配置镜像源。但在本问题中,错误是关于找不到Visual Studio安装,因此主要问题在于编译环境缺失,所以先解决编译环境问题。 如果上述方法无效,还可以考虑直接安装Visual Studio并选择C++桌面开发工作负载。可以下载Visual Studio Build Tools(免费)或Visual Studio Community Edition(免费),并在安装时勾选“使用C++的桌面开发”工作负载。 总结步骤: 1. 安装windows-build-tools(推荐首先尝试的方法)。 2. 如果步骤1无效,则手动安装Visual Studio构建工具(包含C++开发工具)。 3. 完成环境配置后,重新安装ffi-napi。 最后,尝试安装ffi-napi: ``` npm install ffi-napi ``` 注意:如果项目在内网环境,可能需要配置npm代理或镜像,但根据引用[1]的描述,此错误主要是编译环境缺失导致,因此重点在环境搭建。 相关问题: 1. 如何检查windows-build-tools是否安装成功? 2. 安装windows-build-tools时卡住怎么办? 3. 除了windows-build-tools,还有其他方法解决gyp找不到Visual Studio的问题吗? </think> ### 解决 npm 安装 ffi-napigyp 报错找不到 Visual Studio 的问题 根据引用内容的分析,该错误通常是由于缺少 C++ 编译环境造成的。以下是完整的解决方案: #### 关键原因分析 [^1][^3] 1. **node-gyp 依赖 Visual Studio 编译工具**:node-gyp 需要 C++ 编译环境构建本地插件 2. **ffi-napi 是原生模块**:需要编译后才能使用 3. **系统缺失必要组件**:缺少 Windows Build Tools 或 Visual Studio 构建环境 #### 解决方案步骤 1. **安装 Windows Build Tools**(引用[4]方案): ```powershell # 以管理员身份运行 PowerShell npm install --global windows-build-tools ``` - ✅ 自动安装 Python 2.7 和 Visual C++ 构建工具 - ⚠️ 安装过程可能耗时较长(约10-30分钟),请勿中断 - 💡 完成后检查 `%USERPROFILE%\.windows-build-tools` 目录 2. **配置环境变量**(引用[4]补充): - 将 Python 安装路径加入系统环境变量: ``` %USERPROFILE%\.windows-build-tools\python27 ``` - 重启命令行使配置生效 3. **安装 Visual Studio 构建工具**(备选方案): - 下载 [Visual Studio Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) - 安装时勾选: - "使用 C++ 的桌面开发" - Windows 10 SDK(或当前系统对应版本) 4. **清理缓存后重试安装**: ```powershell npm cache clean --force npm install ffi-napi --build-from-source ``` #### 验证安装结果 ```powershell # 检查编译环境 node-gyp configure --verbose # 测试安装 npm list ffi-napi ``` #### 常见问题处理 1. **网络限制环境下**(引用[1]方案): ```powershell npm config set registry https://registry.npmjs.org/ npm config set strict-ssl false ``` 2. **权限问题**: - 始终使用管理员身份运行命令行 - 关闭杀毒软件实时防护(特别是 Windows Defender) 3. **Python 版本冲突**: ```powershell npm config set python "%USERPROFILE%\.windows-build-tools\python27\python.exe" ``` > **关键提示**:Electron 项目需确保 node-gypElectron 版本匹配,可使用 `electron-rebuild` 重新编译原生模块 [^2] ###
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值