【问题标题】:Electron/Webpack not reading .node filesElectron/Webpack 不读取 .node 文件
【发布时间】:2021-10-19 00:46:19
【问题描述】:

我试图用electron 加载robotjs,但我不断收到一个恼人的Failed to compile 错误。

我使用Vue.js 作为界面,如果这很重要的话。

错误

 error  in ./node_modules/robotjs/build/Release/robotjs.node

Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

我尝试使用node-loaderwebpack 添加新规则,但没有奏效。

import MiniCssExtractPlugin from "mini-css-extract-plugin";

module.exports = {
  mode: "development",
  devtool: "source-map",
  target: "node",
  node: {
    __dirname: false,
  },
  resolve: {
    extensions: [".ts", ".js"],
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: "node-loader",
      },
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        use: {
          loader: "ts-loader",
        },
      },
      {
        test: /\.scss$/,
        use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
      },
    ],
  },
  plugins: [new MiniCssExtractPlugin()],
};

【问题讨论】:

  • 你是如何导入robotjs的?
  • 在一个 Vue 组件中,从 "robotjs" 导入机器人;

标签: webpack electron robotjs


【解决方案1】:

你需要告诉 webpack 不要在包中包含 robotjs 而是需要它作为一个 commonjs 模块。

将此行添加到您的 webpack 配置文件中:

module.exports = {
  ...
  externals: {
    robotjs: 'commonjs robotjs',
  },
  ...
}

关于文档:https://webpack.js.org/configuration/externals/#string

【讨论】:

  • 这次的输出是什么?
  • 没有改变,我一直有同样的错误。
猜你喜欢
  • 2021-04-27
  • 1970-01-01
  • 2016-09-17
  • 2014-05-20
  • 2019-01-19
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多