2015-09-01 02:22:06 +00:00
|
|
|
# Using Native Node Modules
|
2013-09-09 07:35:57 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
The native Node modules are supported by Electron, but since Electron is very
|
|
|
|
likely to use a different V8 version from the Node binary installed in your
|
|
|
|
system, you have to manually specify the location of Electron's headers when
|
|
|
|
building native modules.
|
2014-09-08 07:07:33 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
## How to install native modules
|
2014-04-30 07:03:14 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
Three ways to install native modules:
|
2015-10-05 14:48:48 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
### Using `npm`
|
2014-04-30 07:03:14 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
By setting a few environment variables, you can use `npm` to install modules
|
|
|
|
directly.
|
2015-09-01 02:22:06 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
An example of installing all dependencies for Electron:
|
2014-02-20 10:51:57 +00:00
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2016-06-16 08:10:57 +00:00
|
|
|
# Electron's version.
|
|
|
|
export npm_config_target=1.2.3
|
|
|
|
# The architecture of Electron, can be ia32 or x64.
|
|
|
|
export npm_config_arch=x64
|
2016-09-22 15:46:31 +00:00
|
|
|
export npm_config_target_arch=x64
|
2016-06-16 08:10:57 +00:00
|
|
|
# Download headers for Electron.
|
2016-11-04 21:13:15 +00:00
|
|
|
export npm_config_disturl=https://atom.io/download/electron
|
2016-06-16 08:10:57 +00:00
|
|
|
# Tell node-pre-gyp that we are building for 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
|
|
|
|
```
|
2015-05-05 05:39:55 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
### Installing modules and rebuilding for Electron
|
2015-05-05 05:39:55 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
You can also choose to install modules like other Node projects, and then
|
|
|
|
rebuild the modules for Electron with the [`electron-rebuild`][electron-rebuild]
|
|
|
|
package. This module can get the version of Electron and handle the manual steps
|
2016-09-01 14:49:36 +00:00
|
|
|
of downloading headers and building native modules for your app.
|
2016-06-16 08:10:57 +00:00
|
|
|
|
|
|
|
An example of installing `electron-rebuild` and then rebuild modules with it:
|
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2015-05-05 05:39:55 +00:00
|
|
|
npm install --save-dev electron-rebuild
|
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
# Every time you run "npm install", run this:
|
2015-10-23 08:37:51 +00:00
|
|
|
./node_modules/.bin/electron-rebuild
|
2015-10-31 15:57:04 +00:00
|
|
|
|
|
|
|
# On Windows if you have trouble, try:
|
|
|
|
.\node_modules\.bin\electron-rebuild.cmd
|
2015-05-05 05:39:55 +00:00
|
|
|
```
|
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
### Manually building for Electron
|
2014-02-20 10:51:57 +00:00
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
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:
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2017-11-24 10:13:57 +00:00
|
|
|
```sh
|
2016-06-16 08:10:57 +00:00
|
|
|
cd /path-to-module/
|
2016-11-04 21:13:15 +00:00
|
|
|
HOME=~/.electron-gyp node-gyp rebuild --target=1.2.3 --arch=x64 --dist-url=https://atom.io/download/electron
|
2013-08-14 22:43:35 +00:00
|
|
|
```
|
|
|
|
|
2015-04-16 03:31:12 +00:00
|
|
|
The `HOME=~/.electron-gyp` changes where to find development headers. The
|
2016-06-16 08:10:57 +00:00
|
|
|
`--target=1.2.3` is version of Electron. The `--dist-url=...` specifies
|
2015-07-06 12:05:15 +00:00
|
|
|
where to download the headers. The `--arch=x64` says the module is built for
|
2015-05-01 18:28:17 +00:00
|
|
|
64bit system.
|
2016-06-16 08:10:57 +00:00
|
|
|
|
|
|
|
## Troubleshooting
|
|
|
|
|
|
|
|
If you installed a native module and found it was not working, you need to check
|
|
|
|
following things:
|
|
|
|
|
|
|
|
* The architecture of module has to match Electron's architecture (ia32 or x64).
|
|
|
|
* After you upgraded Electron, you usually need to rebuild the modules.
|
|
|
|
* When in doubt, run `electron-rebuild` first.
|
|
|
|
|
2016-12-10 19:23:33 +00:00
|
|
|
## Modules that rely on `prebuild`
|
|
|
|
|
|
|
|
[`prebuild`](https://github.com/mafintosh/prebuild) provides a way to easily
|
|
|
|
publish native Node modules with prebuilt binaries for multiple versions of Node
|
|
|
|
and Electron.
|
|
|
|
|
|
|
|
If modules provide binaries for the usage in Electron, make sure to omit
|
|
|
|
`--build-from-source` and the `npm_config_build_from_source` environment
|
|
|
|
variable in order to take full advantage of the prebuilt binaries.
|
|
|
|
|
2016-06-16 08:10:57 +00:00
|
|
|
## Modules that rely on `node-pre-gyp`
|
|
|
|
|
|
|
|
The [`node-pre-gyp` tool][node-pre-gyp] provides a way to deploy native Node
|
|
|
|
modules with prebuilt binaries, and many popular modules are using it.
|
|
|
|
|
|
|
|
Usually those modules work fine under Electron, but sometimes when Electron uses
|
|
|
|
a newer version of V8 than Node, and there are ABI changes, bad things may
|
|
|
|
happen. So in general it is recommended to always build native modules from
|
|
|
|
source code.
|
|
|
|
|
|
|
|
If you are following the `npm` way of installing modules, then this is done
|
|
|
|
by default, if not, you have to pass `--build-from-source` to `npm`, or set the
|
|
|
|
`npm_config_build_from_source` environment variable.
|
|
|
|
|
|
|
|
[electron-rebuild]: https://github.com/paulcbetts/electron-rebuild
|
|
|
|
[node-pre-gyp]: https://github.com/mapbox/node-pre-gyp
|