Native module import error in renderer after `yarn package` in Typescript + Webpack template
Pre-flight checklist
- [X] I have read the contribution documentation for this project.
- [X] I agree to follow the code of conduct that this project uses.
- [X] I have searched the issue tracker for a bug that matches the one I want to file, without success.
Electron Forge version
6.0.0-beta.63
Electron version
18.0.0
Operating system
Ubuntu x64
Last known working Electron Forge version
No response
Expected behavior
Using webPreferences: { nodeIntegration: true, contextIsolation: false } in the BrowserWindow constructor I should be able to import a native module in the renderer process, e.g.:
import lzmaNative from 'lzma-native';
console.log({ lzmaNative });
I should then be able to create a packaged app with yarn package and run the binary from the out folder. The console should log the imported lzmaNative object.
Actual behavior
Console error:
Uncaught Error: Cannot find module '/native_modules/prebuilds/linux-x64/node.napi.node'
Steps to reproduce
Please see this repo: https://github.com/chetbox/electron-forge-webpack-native-module-bug
Alternatively:
-
yarn create electron-app my-new-app --template=typescript-webpack && cd my-new-app -
yarn add lzma-native -
yarn add --dev @types/lzma-native - Add
webPreferences: { nodeIntegration: true, contextIsolation: false }to theBrowserWindowconstruction inindex.ts - Add
target: 'electron-renderer',towebpack.renderer.config.js - Add the following to
renderer.tsto import the native module and see that it has succeeded:
import lzmaNative from 'lzma-native';
console.log({ lzmaNative });
Additional information
When running with yarn start the module is imported correctly and I see the log message as expected. 👍🏼
I have tried updating to the Electron Forge beta 63 and @vercel/webpack-asset-relocator-loader to 1.7.2 but I see the same behavior. (See repo linked above.)
I have tried add electron-rebuild but that doesn't seem to help. I'm not sure if I need it?
Note that I see the same behavior when Forge is set up to create an ASAR.
I'm using lzma-native as an example but we are having the same issue with serialport@10 which is the library we actually want to use.