Electron/Webpack not reading .node files

783 Views Asked by At

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()],
};
1

There are 1 best solutions below

2
Autumn On

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