【问题标题】:Webpack not including module with Electron Forge and SerialPortWebpack 不包括带有 Electron Forge 和 SerialPort 的模块
【发布时间】:2022-06-29 11:28:48
【问题描述】:

我正在使用 electron-forge webpack 模板。 有一些问题,但串行端口工作正常。

但是现在当我运行 make 导出应用程序时,如果我不将 node_modules 文件夹复制到导出的 webpack 文件夹中,当我运行应用程序时它会显示 serialport not found 错误。

我知道我必须在配置文件中做错了什么,但是什么?我觉得少了点什么。

我正在使用const { SerialPort } = eval("require('serialport')");

当使用const { SerialPort } = require('serialport'); 时,我收到错误“没有为平台找到本机构建=win32 arch=x64 runtime=electron abi=103 uv=1 libc=glibc node=16.13.2 electron=18.0.4 webpack=true '

【问题讨论】:

    标签: webpack electron electron-forge node-serialport


    【解决方案1】:

    如果您使用的是electron-forge,您需要了解两件事。

    1. 为了使用像 serialport 这样的原生模块,你必须将这些模块作为外部模块包含在 webpack 配置中。

    2. 如果一个模块被列为外部模块,它将在打包过程中被修剪。因此,在您的 forge 配置中,您需要在构建过程中包含钩子。

    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);
            });
          });
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2018-09-01
      • 2011-08-01
      • 2021-08-28
      • 2018-08-26
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多