0

I'd like to distribute prebuilt binaries for a native Node.js add-on for Electron.

Presumably Node ABI changes between major bumps so I wonder if running node-pre-gyp with the right version of Node.js is gonna cut it or do I have to run electron-rebuild and publish rebuilt binaries?

A little experiment showed that node-pre-gyp couldn't find the compiled binary when running in electron environment. So it seems that electron-rebuild does something to patch that.

pronebird
  • 12,068
  • 5
  • 54
  • 82

2 Answers2

3

I looked through the sources of electron-builder and found that it simply runs install on the package.

So having the following install hook is sufficient enough:

node-pre-gyp install --fallback-to-build

I don't use electron-rebuild directly anymore since I produce the right binaries in the first place, but I run electron-builder install-app-deps which probably does similar.

I don't see any reason to bother with prebuild and prebuild-install at this point. It's two more dependencies that shield what node-pre-gyp and node-gyp both already implement.

I modified my script for travis to run builds for node and electron side by side:

# build for nodejs
- npm install --build-from-source

# build for electron
- npm install --build-from-source --runtime=electron --target=$ELECTRON_VERSION --dist-url=https://atom.io/download/atom-shell

Packaging step has to run twice with the same flags, i.e:

- if [[ "${TRAVIS_TAG}" != "" ]]; then npm run package --verbose; fi
- if [[ "${TRAVIS_TAG}" != "" ]]; then npm run package --runtime=electron --target=$ELECTRON_VERSION --verbose; fi

Both Travis and Appveyor support uploads to Github Releases or S3 so again no gain from using prebuild or node-pre-gyp-github, example for Travis:

deploy:
  provider: releases
  api_key:
    secure: ENCRYPTED_KEY
  file_glob: true
  file: build/stage/$PACKAGE_VERSION/*.tar.gz
  skip_cleanup: true
  on:
    tags: true
pronebird
  • 12,068
  • 5
  • 54
  • 82
1

You can use prebuild to create prebuilt binaries for electron. We use it for leveldown.

https://github.com/prebuild/prebuild

rtn
  • 127,556
  • 20
  • 111
  • 121
  • Cool! And what do you use for your native binary, pure `node-gyp` or `node-pre-gyp`? I wonder because `npm install` will always install the dependencies for node and not electron. So I guess I still need to run `electron-rebuild` which is likely to copy the node binaries if they are available, is there any point to have binaries for electron at all then. – pronebird Mar 11 '18 at 10:30
  • Correct! You still need `electron-rebuild` and it's still useful with prebuilt for `electron`. We have a demo example you can check out here https://github.com/Level/electron-demo – rtn Mar 11 '18 at 12:07
  • I had to dig into `electron-rebuild` a bit. It has support for `prebuild-install`, so if your module depends on and uses `prebuild-install`, then `electron-rebuild` will download prebuilt binaries instead of building them. – rtn Mar 11 '18 at 12:22
  • 1
    Thanks for your answer. It did help me to find the answer and figure out how that whole thing works. But I decided to stick to node-pre-gyp/node-gyp instruments. – pronebird Mar 13 '18 at 11:32