1

Im trying to load robotjs with electron but I keep getting an annoying Failed to compile error.

I'm using Vue.js for the interface, if that matters.

The error

 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)

I tried adding a new rule using node-loader to webpack but that didn't work.

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()],
};
Leonardo Bezerra
  • 668
  • 1
  • 10
  • 20

1 Answers1

0

You need to tell webpack to not include robotjs on the bundle and instead require it as a commonjs module.

Add this line to your webpack config file:

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

On the documentation: https://webpack.js.org/configuration/externals/#string

Autumn
  • 325
  • 3
  • 13