Rafael Gritti
Rafael Gritti

Reputation: 3

Webpack not including module with Electron Forge and SerialPort

I'm using the electron-forge webpack template. Had some issues but got serialport working.

But now when I run make to export the app, if I don't copy the node_modules folder to the exported webpack folder, when I run the app it shows serialport not found error.

I know that I must be doing something wrong in the config file, but what? I feel that's something missing.

I'm using const { SerialPort } = eval("require('serialport')");

When using const { SerialPort } = require('serialport'); I get the error 'No native build was found for platform=win32 arch=x64 runtime=electron abi=103 uv=1 libc=glibc node=16.13.2 electron=18.0.4 webpack=true'

Upvotes: 0

Views: 1838

Answers (2)

Karol Malinski
Karol Malinski

Reputation: 1

Thank you for this answer. It helped me a lot. More information regarding hooks is found here.

I needed to modify the code a bit.

In readPackageJson hook removed all the scripts as postInstall and prepare caused errors while installing dependencies in the ./out:

delete packageJson['scripts'];
return packageJson;

And in packageAfterPrune hook:

// added flag '--omit=dev' to install only prod dependencies
 const npmInstall = spawn('npm', ['install', '--omit=dev'], { 
          cwd: buildPath,
          stdio: 'inherit',
          shell: true
        });

Upvotes: 0

Adam Ismail
Adam Ismail

Reputation: 26

If you are using electron-forge there are 2 things you need to understand.

  1. In order to use native modules like serialport, you have to include the modules as an external module in webpack config.

  2. If a module is listed as external, it will be pruned during the packaging process. So in your forge config, you need to include hooks during your building process.

hooks: {
    readPackageJson: async (forgeConfig, packageJson) => {
      // only copy deps if there isn't any
      if (Object.keys(packageJson.dependencies).length === 0) {
        const originalPackageJson = await fs.readJson(path.resolve(__dirname, 'package.json'));
        const webpackConfigJs = require('./webpack.renderer.config.js');
        Object.keys(webpackConfigJs.externals).forEach(package => {
          packageJson.dependencies[package] = originalPackageJson.dependencies[package];
        });
      }
      return packageJson;
    },
    packageAfterPrune: async (forgeConfig, buildPath) => {
      console.log(buildPath);
      return new Promise((resolve, reject) => {
        const npmInstall = spawn('npm', ['install'], {
          cwd: buildPath,
          stdio: 'inherit',
          shell: true
        });

        npmInstall.on('close', (code) => {
          if (code === 0) {
            resolve();
          } else {
            reject(new Error('process finished with error code ' + code));
          }
        });

        npmInstall.on('error', (error) => {
          reject(error);
        });
      });
    }
  }

Upvotes: 1

Related Questions