I can't include the RobotJS module in my ElectronJS project

910 views Asked by At

I am developing a desktop application for Windows using ElectronJS. I want to include the RobotJS module in my project, but I can't seem to figure out how to do it. I successfully downloaded the module by running 'npm install robotjs' and called it in the main.js file. However, when I try to launch the application, I get the error shown in the image below. What could be causing this issue? I would be very grateful if you could help me solve my problem. If there is any additional information you need to learn in order to solve the issue, please let me know and I will send it quickly. Thank you in advance for your assistance!

npm version:

Error:

main.js file:

const { app, BrowserWindow, Tray, Menu, ipcMain } = require('electron');
const path = require('path');
var robot = require("robotjs");


let mainWindow = null;
let tray = null;

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 1182,
    height: 503,
    show: true,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    },
    autoHideMenuBar: true,
    resizable: false,
    fullscreenable: false
  });

  mainWindow.loadFile('index.html');

  mainWindow.on('close', (event) => {
    event.preventDefault();
    mainWindow.hide();
  });
}

function createTray() {
  tray = new Tray(path.join(__dirname, 'img/cf.ico'));

  const contextMenu = Menu.buildFromTemplate([
    {
      label: 'Exit',
      click: () => {
        app.exit();
      },
    },
  ]);

  tray.on('click', () => {
    mainWindow.show();
  });

  tray.setToolTip('Casefife Prodeck');
  tray.setContextMenu(contextMenu);
}

app.on('ready', () => {
  createTray();
  createWindow();
});

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow();
  }
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

I can't include the RobotJS module in my ElectronJS project

1

There are 1 answers

7
midnight-coding On

Based on this RobotJS Electron docs page, one needs to know the version of Electron and it's included Node version.

As Electron comes pre-packaged with Node:

You can check what version of Node is used in Electron by looking it up in the releases page or by using process.version (see Quick Start for example). You can find the correct abi version here.

As you are using Electron version 23.1.2, it's pre-packaged Node version is 18.12.1

To rebuild RobotJS for Electron, one needs to use the below code at the command prompt, noting that the questions marks should be replaced with the appropriate values.

npm rebuild --runtime=electron --target=?.?.? --disturl=https://atom.io/download/atom-shell --abi=??

Knowing the version of Electron we are using, we need to find the corresponding Node abi version to use. Referring to the json file found here, there appears to be no reference to Node version 18.12.1

At this moment in time, the most recent version of Node we can use is 17.1.0

As Electron appears to have skipped the use of Node 17, we need to go back a bit further to using Node version 16.

Therefore, to successfully use RobotJS with Electron we need to find a version of Node that both RobotJS and Electron use.

At this moment in time, the most recent RobotJS Node version I can find that will be compatible with the Electron packaged Node version is version 16.13.0

This means that using Electron version 17.4.11 which comes with the pre-packaged Node version 16.13.0 will be compatible with RobotJS when referring to abi version 93

In summary:

  1. Downgrade Electron to version 17.4.11
  2. Ensure RobotJS is installed as a dependency in your package.json file.
  3. Rebuild RobotJS with the below command at the CLI.
npm rebuild --runtime=electron --target=17.4.11 --disturl=https://atom.io/download/atom-shell --abi=93