1

I'm trying to use a native addon (Node Sentinel File Watcher -a.k.a. NSFW-) in a node application and also in an electron application.

Using the same native addon with both (node and electron) doesn't work, so I have to rebuild the addon using electron-rebuild (then it will work with electron) or npm rebuild (then it will work for node), but not for both at the same time.

I'm trying to avoid having to rebuild for switching, and I prefer to have a dynamic require depending on the platform (and I will use one specific version of electron).

Is it possible to install a dependency prebuilding it for two (or more) platforms at the same time?

I found some interesting packages like https://www.npmjs.com/package/prebuild but it looks like you need to modify the native addon (in this case NSFW).

I'm planning to use this as a private NPM package that will be distributed and used for node and for one specific version of electron.

caraie
  • 1,094
  • 7
  • 18
  • Not the biggest expert in N-API here, but I handle lots of different production build setups in a monorepo (with NodeJS APIs, React Native, Vue and CLIs). I've fought this war for some time now and the experience I have is to create separate packages for each target, with third package acting as a common layer for shareable code. It requires more work but can be accomplished relatively easier than inside a single package (retargeting builds in runtime for example with a postinstall script). I can elaborate more with more info, is it a prod or dev dependency? Is it needed in the bundled app? – Luís Brito Jun 12 '20 at 21:43
  • I know the question is old, but may still be relevant for future visitors. – Luís Brito Jun 12 '20 at 21:44
  • 1
    Thank you Luis, I added an answer about how I resolved it (it was a long time ago), I agree it's possible to solve it manually without any lib, generating several builds and requiring it in runtime (it's what node-pre-gyp does). – caraie Jun 13 '20 at 02:44

1 Answers1

1

What I did was to fork the library and used prebuildify to generate several prebuilds; another alternative was to use node-pre-gyp but there are some problems with it and webpack, so I prefered to use prebuildify instead.

caraie
  • 1,094
  • 7
  • 18