认识一下Electron中的.node模块

Node原生模块

.node 文件是一种用于 Node.js 的原生模块。原生模块是一种能在 Node.js 中使用的二进制插件,它通常使用 C 或 C++ 编写,其他的如 Rust 也可以编写,并且编译为 .node 文件。再node代码中可以通过 require() 函数来加载和使用这些插件,就像 JavaScript 模块一样。使用其中暴漏出来的一些api能力。

比如 用于windows环境下置顶窗口的一个库 github.com/robinwassen… ,就是一个Node模块功能库。

Node模块和Electron的一些关系

.node 在 Electron中 应用的一些名词解释

  1. 编译:因为 .node 文件是二进制的,所以它们需要针对特定的 Node.js 版本和架构进行编译。这是因为 Node.js 和 Electron 使用 V8 JavaScript 引擎的 ABI (Application Binary Interface),而 ABI 随着 Node.js 和 Electron 版本的改变而改变。如果你在不同的环境中使用 .node 文件,可能需要为每个环境单独编译。

  2. Electron 版本:由于 Electron 包含了 Node.js,所以它有自己的 Node.js 版本和 ABI。如果你正在使用 Electron,你需要确保你的 .node 文件是针对你的 Electron 版本编译的,而不是你的系统的 Node.js 版本。

  3. electron-rebuild:由于上述原因,如果你的 Electron 应用依赖于原生插件,那么当你改变 Electron 版本时,你需要重新编译这些插件。这就是 electron-rebuild 包的用途。它可以帮助你重新编译所有的原生插件以匹配你的 Electron 版本。

  4. node-gypnode-gyp 是一个跨平台的工具,用于编译 Node.js 的原生插件。如果你在开发或者构建一个包含 .node 文件的 Electron 应用,你可能需要在你的机器上安装和配置 node-gyp

总的来说, 打包好的 .node 要和此node文件运行环境比如electron的 node环境一致,如果不一致,会出现大体如下的错误

Error: The module '/path/to/native/module.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION $XYZ. This version of Node.js requires
NODE_MODULE_VERSION $ABC. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

要避免这类问题,就要保持打包.node 和运行.node时的node环境一致, Node版本对应的 NODE_MODULE_VERSION 可在 nodejs.org/en/download… 上进行简单查阅。

人为的去保证打包和使用原生模块的Node环境时一件很繁琐的事情,而 electron-rebuild就是为了解决这一事情,它可以快速的帮助开发者打包当前Electron对应Node版本的原生文件,而是开发者手动的查阅,切换到对应的Node版本。

node-gyp就是我们要打包.node文件时的构建工具,不管时自己打包,还是使用electron-rebuild,都需要它。

node-gyp

electron-rebuild官网说到,node-gyp时打包.node文件的前提。

Node v12.13.0 or higher is required. Building native modules from source uses node-gyp, refer to the link for its installation/runtime requirements.

node-gyp在windows和mac上的处理有所区别

  • mac上需要python环境和一些xcode配置;
  • windows上需要python环境和  Visual Studio的一些安装处理, 详见下文

macos

在macos上 的 node-gyp 安装方法可以参考 eliu.github.io/2020/02/27/… 此博客

windows

node-gyp官网所言 github.com/nodejs/node…

安装 Visual C++ 构建环境:Visual Studio Build Tools(如果使用早于 VS2019 的版本,则使用“Visual C++ 构建工具”,否则使用“使用 C++ 进行桌面开发”工作负载)或 Visual Studio Community(使用“使用 C++ 进行桌面开发”工作负载) )
python和 C++ Build Tools的安装可以使用工具 windows-build-tools 进行

目前在安装node的时候,windows-build-tools已经集成进Node的安装包内了,如下图
认识一下Electron中的.node模块

windows下找不到Visual Studio的问题

安装 Visual Studio Community

在electron打包forcefocus时,非预期的出现了Could not find any Visual Studio的错误

认识一下Electron中的.node模块

解决问题思路是,检查 Visual Studio 是否安装,查到文件夹中其实只装了 BuildTools ,并没有装 Community,去官网下载 installer,按提示安装Community即可
认识一下Electron中的.node模块

注意:安装的时候记得点击勾选使用C++的桌面开发这一项
认识一下Electron中的.node模块

安装完毕后, Visual Studio的文件夹就有了 Community目录
认识一下Electron中的.node模块

打开electron-rebuild debug模式

// 项目package.json示例
"rebuild": "node-gyp rebuild",
"rebuild-electron": "electron-rebuild -f",
"install": "prebuild-install || node-gyp rebuild",   

满心欢喜的执行 npm run rebuild-electron,还是错误 Could not find any Visual Studio
认识一下Electron中的.node模块

按照错误提示,我打开了electron-rebuild的debug模式,如何打开见以下代码,@chatgpt4


要将 DEBUG 环境变量设置为 “electron-rebuild”,你可以在命令行使用下面的命令。具体的命令会根据你使用的操作系统和终端不同而有所不同。

如果你在 Unix 或者类 Unix 系统(比如 Linux 或 macOS)的 Bash 或者类似的 shell 中:

DEBUG=electron-rebuild npm run rebuild-electron

如果你在 Windows 的 cmd.exe 中:

set DEBUG=electron-rebuild && npm run rebuild-electron

如果你在 Windows 的 PowerShell 中:

$env:DEBUG='electron-rebuild'; npm run rebuild-electron

这些命令会设置 DEBUG 环境变量并立即运行 npm run rebuild-electron。设置的环境变量只对这个命令有效,不会影响其他的终端会话或者命令。

如果 “electron-rebuild” 提供了更多的 debug 信息,你应该能在你的终端输出中看到。


通过debug模式的log ,找到了问题所在

认识一下Electron中的.node模块

认识一下Electron中的.node模块

可以清楚的看到,node-gyp执行的时候找的不是 Visual Studio 2022(我安装的版本), 而是 Visual Studio 2015,找的到才怪哦。

如果你在使用electron-rebuild的时候遇到非预期问题,可以通过debug模式来查看详细信息

配置node-gyp的 Visual Studio版本

问题找到了,接下来改去配置node-gyp执行过程中,索引到正确的vs版本即可。

在网上查阅了一些帖子,大部分都是说让设置npm cofing的设置 如下,

// .npmrc文件
msvs_version=2022
msvs-version=2022

但是不起作用,之后在electron-rebuild的github issue 中找到了答案:
认识一下Electron中的.node模块

简单来讲,npm的vs版本设置和node-gyp无关,需要设置GYP_MSVS_VERSION

问题解决

使用 命令 GYP_MSVS_VERSION=2022 npm run rebuild-electron 解决问题,当然,最好的办法是把系统的GYP_MSVS_VERSION 变量全局更改。

认识一下Electron中的.node模块

.node文件如愿打包,其中114 116即不同版本的electron对应的node环境不同而导致的NODE_MODULE_VERSION不同

原文链接:https://juejin.cn/post/7257441061873139773 作者:noobakong

(1)
上一篇 2023年7月20日 上午10:51
下一篇 2023年7月20日 上午11:02

相关推荐

发表回复

登录后才能评论