WebPack不包括电子锻造和SerialPort的模块

发布于 2025-01-22 11:10:02 字数 466 浏览 6 评论 0原文

我正在使用电子福用Webpack模板。 遇到了一些问题,但有序列上的工作。

但是现在,当我运行以导出该应用程序时,如果我不将Node_modules文件夹复制到导出的WebPack文件夹,则在运行应用程序时,将显示serialport显示出错误。

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

我正在使用const {serialport} = est(“ require('requiel('serialport')”);

使用const {serialport} = require('SerialPort'); i获取错误'未找到平台= Win32 Arch = x64 runtime = electron abi = 103 uv = 1 libc = glibc node = 16.13.2 electron = 18.0.0 webpack = true'true'

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2025-01-29 11:10:02

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

  1. 为了使用serialport之类的本机模块,您必须将模块作为外部模块包含在WebPack配置中。

  2. 如果将模块列为外部,则将在包装过程中修剪。因此,在锻造配置中,您需要在建筑过程中包括钩子。

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

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);
        });
      });
    }
  }
爱她像谁 2025-01-29 11:10:02

谢谢您的答案。它对我有很大帮助。找到有关钩子的更多信息,请找到在这里

我需要对代码进行一些修改。

中,readpackagejson挂钩将所有脚本删除为post -install and 准备./ OUT中安装依赖项时会导致错误:

delete packageJson['scripts'];
return packageJson;

packagewterprune钩子:

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

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
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文