简体   繁体   English

在 Electron 应用程序中,使用 webpack/electron-builder 到 package 后,在 worker_threads 提示 MODULE_NOT_FOUND 中需要第三方模块

[英]In Electron app, after using webpack/electron-builder to package, require third-party module in worker_threads prompts MODULE_NOT_FOUND

ENV:环境:

  • Electron: v11.0.3 Electron:v11.0.3
  • Node: v14.15.1节点:v14.15.1
  • OS: win10 2020 x64操作系统:win10 2020 x64

package.json - build package.json - 构建

"build":{
  "productName": "myapp",
  "directories": {
    "output": "build"
  },
  "files": [
    "dist/electron/**/*"
  ]
}

Webpack.json Webpack.json

{
    entry: {
      main: path.join(__dirname, '../src/main/index.js')
    },
    externals: [
      ...Object.keys(dependencies || {})
    ],
    module: {
      rules: [
        {
          test: /\.js$/,
          use: 'babel-loader',
          exclude: /node_modules/
        },
        {
          test: /\.node$/,
          use: 'node-loader'
        }
      ]
    },
    node: {
      __dirname: process.env.NODE_ENV !== 'production',
      __filename: process.env.NODE_ENV !== 'production'
    },
    output: {
      filename: '[name].js',
      libraryTarget: 'commonjs2',
      path: path.join(__dirname, '../dist/electron')
    },
    plugins: [
      new webpack.ExternalsPlugin("commonjs", ["node-hid"])
    ],
    resolve: {
      extensions: ['.js', '.json', '.node']
    },
    target: 'electron-main'
  }

index.js index.js

const { Worker } = require('worker_threads');
const worker = new Worker(`const HID = require("node-hid");console.log("hello");`, { eval:true });
worker.on('error', (err) => {
    console.log('worker error:',err);
});

The above code can output "hello" normally when running in the development environment, but when I run it after packaging it will output the following error:上述代码在开发环境中运行时可以正常运行output "hello",但是打包后运行时会出现output如下错误:

worker error:
{code: "MODULE_NOT_FOUND", requireStack: ["C:\Users\user\AppData\Local\Programs\myapp\[worker eval]"]}

Should I use asarUnpack related configuration?我应该使用asarUnpack相关配置吗?

In the end I used asarUnpack to solve this problem:最后我使用 asarUnpack 解决了这个问题:

index.js index.js

const { Worker } = require('worker_threads');
const worker = new Worker(`
    const path = require("path");
    function dynamicallyRequire(moduleName) {
        let modulePath = getNodeModulesPath(moduleName);
        let module = require(modulePath);
        return module;
    }
    function getNodeModulesPath(moduleName) {
        return workerData.env === 'development' ? moduleName : path.join(process.cwd(), 'resources\\app.asar.unpacked\\node_modules\\' + moduleName);
    }
    const HID = dynamicallyRequire("node-hid");
    console.log("hello");
`, { eval:true, workerData: { env: process.env.NODE_ENV }});
worker.on('error', (err) => {
    console.log('worker error:',err);
});

package.json --> build --> asarUnpack package.json --> 构建 --> asarUnpack

"asarUnpack": [
  "./node_modules/node-hid/**/*",
  "./node_modules/node-addon-api/**/*",
  "./node_modules/bindings/**/*",
  "./node_modules/file-uri-to-path/**/*"
]

It is worth mentioning that the dependencies of node-hid also need to be included, such as bindings , node-addon-api , file-uri-to-path值得一提的是 node-hid 的依赖也需要包含,比如bindings , node-addon-api , file-uri-to-path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack /电子要求动态模块 - Webpack/electron require dynamic module 在电子锻造 + webpack 应用程序中使用电子构建器构建的应用程序显示空白屏幕 - App built using electron-builder in electron-forge + webpack application shows blank screen 结合 node (v11.15.0)、webpack 和 typescript 使用 worker_threads -> 找不到模块 - Use worker_threads in combination with node (v11.15.0), webpack and typescript -> module not found electron-builder 安装程序:主进程出现 JavaScript 错误,错误:找不到指定的模块 - electron-builder installer: A JavaScript error occurred in the main process, Error: The specified module could not be found 如何在Electron-builder中使用相对路径包含Node.js模块 - How to include a Node.js module using a relative path with Electron-builder 与nodejs worker_threads模块一起使用时广播频道问题 - Issue with broadcast channel when using with nodejs worker_threads module Typescript 找不到模块“worker_threads” - Typescript cannot find module 'worker_threads' 运行 electron-builder 后,Puppeteer 在 React 应用程序中失败 - Puppeteer fails in React app after running electron-builder 如何使用电子生成器 package 预编译二进制文件 - How to package precompiled binary with electron-builder 在使用electronic-builder构建的电子应用程序中包含Bootstrap - Including Bootstrap in an electron app built with electron-builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM