In this section we learn how to use the Node
native module Electron
Electron
support native Node
module, but the official and Node
comparison, Electron
possible to use a system and we have installed Node
different V8
engine, the module used need to be recompiled to use. If we want to compile a native module, we need to manually set the location of Electron
of headers
How to install native modules
There are three ways to install native modules, they are:
- Install and recompile the module for
Electron
- Install native modules through
npm
- Manually compile for
Electron
Install and recompile the module for Electron
The easiest way is to Electron
electron-rebuild
package. The module can automatically determine Electron
, and process the steps of downloading headers
and rebuilding the native module for the application.
Example:
For example, electron-rebuild
, you first need to install electron-rebuild
:
npm install --save-dev electron-rebuild
Every time npm install
is run, the following command will be run at the same time:
./node_modules/.bin/electron-rebuild
If you encounter problems with the above commands under windows
, you can try to execute the following commands:
.\node_modules\.bin\electron-rebuild.cmd
Install via npm
We can also install native modules directly npm
Most of the steps are the same as when installing ordinary modules, but you need to set some system environment variables yourself.
Example:
For example, to install all the dependencies of Electron
# Electron的版本
export npm_config_target=1.2.3
# Electron的目标架构
export npm_config_arch=x64
export npm_config_target_arch=x64
# 下载Electron的headers
export npm_config_disturl=https://electronjs.org/headers
# 告诉node-pre-gyp我们是在为Electron生成模块
export npm_config_runtime=electron
# 告诉node-pre-gyp从源代码构建模块
export npm_config_build_from_source=true
# 安装所有依赖,并缓存到 ~/.electron-gyp
HOME=~/.electron-gyp npm install
Manually compile for Electron
Developers of native modules who want Electron
may have to manually compile the Electron
module. You can use node-gyp
to compile directly.
Example:
For example, we want to tell node-gyp
where to download Electron
of headers
, and what version to download:
$ cd /path-to-module/
$ HOME=~/.electron-gyp node-gyp rebuild --target=0.29.1 --arch=x64 --dist-url=https://atom.io/download/atom-shell
HOME=~/.electron-gyp
: Set where to findheaders
during development.--target=0.29.1
: The versionElectron
--dist-url=...
: Set the download addressElectron
ofheaders
--arch=x64
: Set the module to compile for 64-bit operating system.
Link: https://www.9xkd.com/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。