UNPKG

6.89 kBMarkdownView Raw
1## Electron Rebuild
2
3[![CircleCI](https://circleci.com/gh/electron/electron-rebuild.svg?style=svg)](https://circleci.com/gh/electron/electron-rebuild)
4[![NPM](https://img.shields.io/npm/v/electron-rebuild.svg?style=flat)](https://npm.im/electron-rebuild)
5[![Coverage Status](https://codecov.io/gh/electron/electron-rebuild/branch/master/graph/badge.svg)](https://codecov.io/gh/electron/electron-rebuild)
6
7This executable rebuilds native Node.js modules against the version of Node.js
8that your Electron project is using. This allows you to use native Node.js
9modules in Electron apps without your system version of Node.js matching exactly
10(which is often not the case, and sometimes not even possible).
11
12### How does it work?
13
14Install the package with `--save-dev`:
15
16```sh
17npm install --save-dev electron-rebuild
18```
19
20Then, whenever you install a new npm package, rerun electron-rebuild:
21
22```sh
23$(npm bin)/electron-rebuild
24```
25
26Or if you're on Windows:
27
28```sh
29.\node_modules\.bin\electron-rebuild.cmd
30```
31If 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:
32
33```json
34"scripts": {
35 "rebuild": "electron-rebuild -f -w yourmodule"
36}
37```
38
39and then
40
41```sh
42npm run rebuild
43```
44
45### What are the requirements?
46
47Node v10.12.0 or higher is required. Building the native modules from source uses
48[`node-gyp`](https://github.com/nodejs/node-gyp#installation), refer to the link for its
49installation/runtime requirements.
50
51### CLI Arguments
52
53```
54Usage: electron-rebuild --version [version] --module-dir [path]
55
56Options:
57 -h, --help Show help [boolean]
58 -v, --version The version of Electron to build against
59 -f, --force Force rebuilding modules, even if we would skip
60 it otherwise
61 -a, --arch Override the target architecture to something
62 other than your system's
63 -m, --module-dir The path to the app directory to rebuild
64 -w, --which-module A specific module to build, or comma separated
65 list of modules. Modules will only be rebuilt if they
66 also match the types of dependencies being rebuilt
67 (see --types).
68 -e, --electron-prebuilt-dir The path to electron-prebuilt
69 -d, --dist-url Custom header tarball URL
70 -t, --types The types of dependencies to rebuild. Comma
71 separated list of "prod", "dev" and "optional".
72 Default is "prod,optional"
73 -p, --parallel Rebuild in parallel, this is enabled by default
74 on macOS and Linux
75 -s, --sequential Rebuild modules sequentially, this is enabled by
76 default on Windows
77 -o, --only Only build specified module, or comma separated
78 list of modules. All others are ignored.
79 -b, --debug Build debug version of modules
80 --prebuild-tag-prefix GitHub tag prefix passed to prebuild-install.
81 Default is "v"
82
83Copyright 2016
84```
85
86### How can I use this with [Electron Forge](https://github.com/electron-userland/electron-forge)?
87
88This package is automatically used with Electron Forge when packaging an Electron app.
89
90### How can I integrate this into [Electron Packager](https://github.com/electron-userland/electron-packager)?
91
92electron-rebuild provides a function compatible with the [`afterCopy` hook](https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#aftercopy)
93for Electron Packager. For example:
94
95```javascript
96import packager from 'electron-packager';
97import rebuild from 'electron-rebuild';
98
99packager({
100 // … other options
101 afterCopy: [(buildPath, electronVersion, platform, arch, callback) => {
102 rebuild({ buildPath, electronVersion, arch })
103 .then(() => callback())
104 .catch((error) => callback(error));
105 }],
106 // … other options
107});
108```
109
110### How can I integrate this with [prebuild](https://github.com/prebuild/prebuild)?
111
112If your module uses [prebuild](https://github.com/prebuild/prebuild) for creating prebuilt binaries,
113it also uses [prebuild-install](https://github.com/prebuild/prebuild-install) to download them. If
114this is the case, then `electron-rebuild` will run `prebuild-install` to download the correct
115binaries from the project's GitHub Releases instead of rebuilding them.
116
117### How can I integrate this into Grunt / Gulp / Whatever?
118
119electron-rebuild is also a library that you can require into your app or
120build process. It has a very simple API:
121
122```javascript
123import rebuild from 'electron-rebuild';
124
125// Public: Rebuilds a node_modules directory with the given Electron version.
126//
127// options: Object with the following properties
128// buildPath - An absolute path to your app's directory. (The directory that contains your node_modules)
129// electronVersion - The version of Electron to rebuild for
130// arch (optional) - Default: process.arch - The arch to rebuild for
131// extraModules (optional) - Default: [] - An array of modules to rebuild as well as the detected modules
132// onlyModules (optional) - Default: null - An array of modules to rebuild, ONLY these module names will be rebuilt.
133// The "types" property will be ignored if this option is set.
134// force (optional) - Default: false - Force a rebuild of modules regardless of their current build state
135// headerURL (optional) - Default: https://www.electronjs.org/headers - The URL to download Electron header files from
136// types (optional) - Default: ['prod', 'optional'] - The types of modules to rebuild
137// mode (optional) - The rebuild mode, either 'sequential' or 'parallel' - Default varies per platform (probably shouldn't mess with this one)
138// useElectronClang (optional) - Whether to use the clang executable that Electron used when building its binary. This will guarantee compiler compatibility
139
140// Returns a Promise indicating whether the operation succeeded or not
141```
142
143A full build process might look something like:
144
145```javascript
146const childProcess = require('child_process');
147const pathToElectron = require('electron');
148
149 rebuild({
150 buildPath: __dirname,
151 electronVersion: '1.4.12'
152 })
153 .then(() => console.info('Rebuild Successful'))
154 .catch((e) => {
155 console.error("Building modules didn't work!");
156 console.error(e);
157 });
158```
159
160### Alternatives
161
162- [require-rebuild](https://github.com/juliangruber/require-rebuild) patches `require()` to rebuild native node modules on the fly