Native Node Modules
Native Node.js modules are supported by Electron, but since Electron has a different application binary interface (ABI) from a given Node.js binary (due to differences such as using Chromium's BoringSSL instead of OpenSSL), the native modules you use will need to be recompiled for Electron. Иначе, при запуске приложения вы получите следующий класс ошибок:
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`).
Как установить нативные модули
Существует несколько способов установки нативных модулей:
Installing modules and rebuilding for Electron
You can install modules like other Node projects, and then rebuild the modules for Electron with the @electron/rebuild
package. This module can automatically determine the version of Electron and handle the manual steps of downloading headers and rebuilding native modules for your app. If you are using Electron Forge, this tool is used automatically in both development mode and when making distributables.
For example, to install the standalone @electron/rebuild
tool and then rebuild modules with it via the command line:
npm install --save-dev @electron/rebuild
# Every time you run "npm install", run this:
./node_modules/.bin/electron-rebuild
# If you have trouble on Windows, try:
.\node_modules\.bin\electron-rebuild.cmd
For more information on usage and integration with other tools such as Electron Packager, consult the project's README.
При помощи npm
Устанавливая несколько переменных среды, вы можете использовать npm
для непосредственной установки модулей.
For example, to install all dependencies for Electron:
# Electron's version.
export npm_config_target=1.2.3
# The architecture of your machine
export npm_config_arch=x64
export npm_config_target_arch=x64
# Download headers for Electron.
export npm_config_disturl=https://electronjs.org/headers
# Говорит node-pre-gyp, что мы собираем для Electron.
export npm_config_runtime=electron
# Tell node-pre-gyp to build module from source code.
export npm_config_build_from_source=true
# Install all dependencies, and store cache to ~/.electron-gyp.
HOME=~/.electron-gyp npm install
Manually building for Electron
If you are a developer developing a native module and want to test it against Electron, you might want to rebuild the module for Electron manually. You can use node-gyp
directly to build for Electron:
cd /path-to-module/
HOME=~/.electron-gyp node-gyp rebuild --target=1.2.3 --arch=x64 --dist-url=https://electronjs.org/headers
HOME=~/.electron-gyp
changes where to find development headers.--target=1.2.3
is the version of Electron.--dist-url=...
specifies where to download the headers.--arch=x64
says the module is built for a 64-bit system.
Manually building for a custom build of Electron
To compile native Node modules against a custom build of Electron that doesn't match a public release, instruct npm
to use the version of Node you have bundled with your custom build.
npm rebuild --nodedir=/path/to/src/out/Default/gen/node_headers