Using Node.js addons in Electron's renderer with Webpack

后端 未结 4 1336
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-10 20:23

I have the following renderer:

import SerialPort from \"serialport\";

new SerialPort(\"/dev/tty-usbserial1\", { baudRate: 57600 });

It\'s buil

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-10 21:02

    I wanna thank @Alec Mev for practically the only answer on the entire internet that truly works and has real knowledge inside. I've spent days troubleshooting issues with webpack and native modules for electron until I tried Alec's answer and it perfectly works.

    Just a small addition, that if the native code is being used within the main process and not the renderer (most common case when app security is stronger), the shim needs to be adjusted as following:

    module.exports = x =>
      __non_webpack_require__(
        `${require('electron').app.getAppPath()}/${x}`
      );
    

提交回复
热议问题