使用 Node 原生模块

Electron 同样也支持 Node 原生模块,但由于和官方的 Node 相比使用了不同的 V8 引擎,如果你想编译原生模块,则需要手动设置 Electron 的 headers 的位置。

如何安装原生模块

如下三种方法教你安装原生模块

通过 npm 安装

只要设置一些系统环境变量,你就可以通过 npm 直接安装原生模块。

为 Electron 安装所有依赖项的一个例子:

  1. # Electron 的版本。
  2. export npm_config_target=1.2.3
  3. # Electron 的系统架构, 值为 ia32 或者 x64。
  4. export npm_config_arch=x64
  5. export npm_config_target_arch=x64
  6. # 下载 Electron 的 headers。
  7. export npm_config_disturl=https://atom.io/download/electron
  8. # 告诉 node-pre-gyp 我们是在为 Electron 生成模块。
  9. export npm_config_runtime=electron
  10. # 告诉 node-pre-gyp 从源代码构建模块。
  11. export npm_config_build_from_source=true
  12. # 安装所有依赖,并缓存到 ~/.electron-gyp。
  13. HOME=~/.electron-gyp npm install

为 Electron 安装并重新编译模块

你可以也选择安装其他 Node 项目模块一样,然后用 electron-rebuild 包重建 Electron 模块 。 它可以识别当前 Electron 版本,帮你自动完成了下载 headers、编译原生模块等步骤。

一个下载 electron-rebuild 并重新编译的例子:

  1. npm install --save-dev electron-rebuild
  2. # 每次运行"npm install"时,也运行这条命令
  3. ./node_modules/.bin/electron-rebuild
  4. # 在windows下如果上述命令遇到了问题,尝试这个:
  5. .\node_modules\.bin\electron-rebuild.cmd

为 Electron 手动编译

如果你是一个原生模块的开发人员,想在 Electron 中进行测试, 你可能要手动编译 Electron 模块。 你可以 使用 node-gyp 直接编译:

  1. cd /path-to-module/
  2. HOME=~/.electron-gyp node-gyp rebuild --target=1.2.3 --arch=x64 --dist-url=https://atom.io/download/electron

HOME=~/.electron-gyp 设置去哪找开发时的 headers。 —target=1.2.3 设置了 Electron 的版本。 —dist-url=…设置了 Electron 的 headers 的下载地址。 —arch=x64 设置了该模块为适配64位操作系统而编译。

为Electron的自定义编译手动编译

针对与公共发行版不匹配的Electron的自定义版本编译原生Node插件,npm要使用与自定义版本对应的Node版本。

  1. npm rebuild --nodedir=$HOME/.../path/to/electron/vendor/node

故障排查

如果你安装了一个原生模块并发现它不能工作,你需要检查 以下事项:

  • 模块的对应的操作系统和 Electron 对应的操作系统是否匹配(ia32 或 x64)。
  • win_delay_load_hook is not set to false in the module's binding.gyp.
  • 如果升级了 Electron,你通常需要重新编译这些模块。
  • 当有疑问时,请先执行 electron-rebuild

A note about win_delay_load_hook

On Windows, by default, node-gyp links native modules against node.dll. However, in Electron 4.x and higher, the symbols needed by native modules are exported by electron.exe, and there is no node.dll in Electron 4.x. In order to load native modules on Windows, node-gyp installs a delay-load hook that triggers when the native module is loaded, and redirects the node.dll reference to use the loading executable instead of looking for node.dll in the library search path (which would turn up nothing). As such, on Electron 4.x and higher, 'win_delay_load_hook': 'true' is required to load native modules.

If you get an error like Module did not self-register, or The specified procedure could not be found, it may mean that the module you're trying to use did not correctly include the delay-load hook. If the module is built with node-gyp, ensure that the win_delay_load_hook variable is set to true in the binding.gyp file, and isn't getting overridden anywhere. If the module is built with another system, you'll need to ensure that you build with a delay-load hook installed in the main .node file. Your link.exe invocation should look like this:

  1. link.exe /OUT:"foo.node" "...\node.lib" delayimp.lib /DELAYLOAD:node.exe /DLL
  2. "my_addon.obj" "win_delay_load_hook.obj"

In particular, it's important that:

  • you link against node.lib from Electron and not Node. If you link against the wrong node.lib you will get load-time errors when you require the module in Electron.
  • you include the flag /DELAYLOAD:node.exe. If the node.exe link is not delayed, then the delay-load hook won't get a chance to fire and the node symbols won't be correctly resolved.
  • win_delay_load_hook.obj is linked directly into the final DLL. If the hook is set up in a dependent DLL, it won't fire at the right time.
    See node-gyp for an example delay-load hook if you're implementing your own.

依赖于 prebuild 的模块

prebuild 为多个版本的 Node 和 Electron 提供了一种简单发布预编译二进制原生模块的方法。

如果为 Electron 提供二进制原生模块,请确保删除 —build-from-sourcenpm_config_build_from_source 环境变量 来充分利用预编译的二进制文件。

依赖于 node-pre-gyp 的模块

node-pre-gyp 工具 提供一种部署原生 Node 预编译二进制模块的方法, 许多流行的模块都是使用它。

通常这些模块在 Electron 中工作良好,但有时当 Electron 使用 比 Node 新的 V8 版本时,会有 ABI 改变,可能发生错误。 因此,一般来说,建议始终从源代码编译原生模块。

如果你通过 npm 的方式安装模块,默认情况下这就完成了, 如果没有,你需要传入 —build-from-sourcenpm, 或者设置 npm_config_build_from_source 环境变量。