2017-02-27 01:18:44 +03:00
## Electron Rebuild
2015-05-04 11:44:51 +03:00
2019-02-04 02:05:18 +03:00
[![Build Status ](https://travis-ci.org/electron/electron-rebuild.svg?branch=master )](https://travis-ci.org/electron/electron-rebuild)
2019-02-04 08:11:00 +03:00
[![NPM ](https://img.shields.io/npm/v/electron-rebuild.svg?style=flat )](https://npm.im/electron-rebuild)
2019-02-04 02:05:18 +03:00
2016-07-28 01:38:23 +03:00
This executable rebuilds native Node.js modules against the version of Node.js
that your Electron project is using. This allows you to use native Node.js
modules in Electron apps without your system version of Node.js matching exactly
2015-05-05 08:28:55 +03:00
(which is often not the case, and sometimes not even possible).
### How does it work?
Install the package with `--save-dev` :
```sh
npm install --save-dev electron-rebuild
```
Then, whenever you install a new npm package, rerun electron-rebuild:
```sh
2017-02-27 01:18:44 +03:00
$(npm bin)/electron-rebuild
2015-05-05 08:28:55 +03:00
```
2015-10-31 18:57:40 +03:00
Or if you're on Windows:
```sh
.\node_modules\.bin\electron-rebuild.cmd
```
2017-02-27 01:18:44 +03:00
If you have a good node-gyp config but you see an error about a missing element on Windows like `Could not load the Visual C++ component "VCBuild.exe"` , try to launch electron-rebuild in an npm script:
2016-08-04 23:03:47 +03:00
2016-06-03 13:34:06 +03:00
```json
2016-08-04 23:03:47 +03:00
"scripts": {
2017-02-27 01:18:44 +03:00
"rebuild": "electron-rebuild -f -w yourmodule"
2016-06-03 13:34:06 +03:00
}
```
2016-08-04 23:03:47 +03:00
2016-06-03 13:34:06 +03:00
and then
2016-08-04 23:03:47 +03:00
2016-06-03 13:34:06 +03:00
```sh
npm run rebuild
```
2016-08-04 23:03:47 +03:00
2019-02-04 00:09:54 +03:00
### What versions of Node does it work with?
Node v6.0.0 or higher is required.
2016-12-20 12:21:31 +03:00
### CLI Arguments
```
Usage: electron-rebuild --version [version] --module-dir [path]
Options:
-h, --help Show help [boolean]
-v, --version The version of Electron to build against
-f, --force Force rebuilding modules, even if we would skip
it otherwise
-a, --arch Override the target architecture to something
other than your system's
-m, --module-dir The path to the app directory to rebuild
-w, --which-module A specific module to build, or comma separated
list of modules
-e, --electron-prebuilt-dir The path to electron-prebuilt
-d, --dist-url Custom header tarball URL
-t, --types The types of dependencies to rebuild. Comma
seperated list of "prod", "dev" and "optional".
Default is "prod,optional"
-p, --parallel Rebuild in parallel, this is enabled by default
on macOS and Linux
-s, --sequential Rebuild modules sequentially, this is enabled by
default on Windows
2017-06-28 00:51:09 +03:00
-o, --only Only build specified module, or comma separated
list of modules. All others are ignored.
2017-10-30 12:23:06 +03:00
-b, --debug Build debug version of modules
2016-12-20 12:21:31 +03:00
Copyright 2016
```
2017-02-27 01:53:39 +03:00
### How can I use this with [Electron Forge](https://github.com/electron-userland/electron-forge)?
2017-02-19 08:05:38 +03:00
2017-02-27 01:53:39 +03:00
This package is automatically used with Electron Forge when packaging an Electron app.
2017-02-19 08:05:38 +03:00
2017-02-27 01:53:39 +03:00
### How can I integrate this into [Electron Packager](https://github.com/electron-userland/electron-packager)?
electron-rebuild provides a function compatible with the [`afterCopy` hook ](https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#aftercopy )
for Electron Packager. For example:
```javascript
import packager from 'electron-packager';
2017-02-19 08:05:38 +03:00
import rebuild from 'electron-rebuild';
2017-02-27 01:53:39 +03:00
packager({
// … other options
afterCopy: [(buildPath, electronVersion, platform, arch, callback) => {
2017-06-14 12:10:57 +03:00
rebuild({ buildPath, electronVersion, arch })
2017-02-27 01:53:39 +03:00
.then(() => callback())
.catch((error) => callback(error));
2017-02-19 08:05:38 +03:00
}],
2017-02-27 01:53:39 +03:00
// … other options
});
2017-02-19 08:05:38 +03:00
```
2018-03-12 02:16:33 +03:00
### How can I integrate this with [prebuild](https://github.com/prebuild/prebuild)?
If your module uses [prebuild ](https://github.com/prebuild/prebuild ) for creating prebuilt binaries it also uses [prebuild-install ](https://github.com/prebuild/prebuild-install ) to download them. If this is the case then `electron-rebuild` will run `prebuild-install` to download the correct binaries from github instead of rebuilding them.
2015-05-05 08:28:55 +03:00
### How can I integrate this into Grunt / Gulp / Whatever?
electron-rebuild is also a library that you can just require into your app or
2016-12-20 12:21:31 +03:00
build process. It has a very simple API:
2015-05-05 08:28:55 +03:00
2017-02-27 01:18:44 +03:00
```javascript
2016-12-20 12:21:31 +03:00
import rebuild from 'electron-rebuild';
2015-05-05 08:28:55 +03:00
// Public: Rebuilds a node_modules directory with the given Electron version.
//
2017-06-14 12:10:57 +03:00
// options: Object with the following properties
2017-08-03 23:39:05 +03:00
// buildPath - An absolute path to your app's directory. (The directory that contains your node_modules)
2017-06-14 12:10:57 +03:00
// electronVersion - The version of Electron to rebuild for
// arch (optional) - Default: process.arch - The arch to rebuild for
// extraModules (optional) - Default: [] - An array of modules to rebuild as well as the detected modules
2017-07-30 04:45:58 +03:00
// onlyModules (optional) - Default: null - An array of modules to rebuild, ONLY these module names will be rebuilt.
// The "types" property will be ignored if this option is set.
2017-06-14 12:10:57 +03:00
// force (optional) - Default: false - Force a rebuild of modules regardless of their current build state
// headerURL (optional) - Default: atom.io/download/electron - The URL to download Electron header files from
// types (optional) - Default: ['prod', 'optional'] - The types of modules to rebuild
// mode (optional) - The rebuild mode, either 'sequential' or 'parallel' - Default varies per platform (probably shouldn't mess with this one)
2016-12-20 12:21:31 +03:00
2015-05-05 08:28:55 +03:00
// Returns a Promise indicating whether the operation succeeded or not
```
2015-06-02 01:55:36 +03:00
A full build process might look something like:
2017-02-27 01:18:44 +03:00
```javascript
2016-07-28 01:38:23 +03:00
let childProcess = require('child_process');
let pathToElectron = require('electron-prebuilt');
2017-06-14 12:10:57 +03:00
rebuild({
buildPath: __dirname,
electronVersion: '1.4.12'
})
2016-12-20 12:21:31 +03:00
.then(() => console.info('Rebuild Successful'))
.catch((e) => {
console.error("Building modules didn't work!");
console.error(e);
2016-07-28 01:38:23 +03:00
});
```
2016-02-02 13:06:52 +03:00
2016-02-11 19:16:11 +03:00
### Alternatives
- [require-rebuild ](https://github.com/juliangruber/require-rebuild ) patches `require()` to rebuild native node modules on the fly