1

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

midnight-coding
  • 2,857
  • 2
  • 17
  • 27
  • The error literally is telling you that you have version dependency problem. – gview Mar 10 '23 at 20:04
  • So, do I need to update NodeJS installed on my computer or the robotjs module? I'm not sure which one is causing the problem exactly. :( – olcaykuytak Mar 10 '23 at 21:48
  • Have you rebuilt RobotJS for Electron using the instructions in [this link](https://robotjs.io/docs/electron)? – midnight-coding Mar 10 '23 at 21:53
  • You need to know what version of Electron you are running and what version of Node is packaged within that version of Electron. – midnight-coding Mar 10 '23 at 21:57
  • @midnight-coding I tried it, but it gives me this error that you can see in the image. :( [error image](https://i.hizliresim.com/j9s07j5.png) – olcaykuytak Mar 10 '23 at 22:01
  • What version of Electron are you using? – midnight-coding Mar 10 '23 at 22:37
  • @midnight-coding ElectronJS Version: 23.1.2 - RobotJS: 0.6.0 – olcaykuytak Mar 10 '23 at 22:45
  • @midnight-coding I still get an error when I run this code. You can check [the link](https://i.hizliresim.com/3m279he.png) for details. – olcaykuytak Mar 10 '23 at 22:55
  • Your current version of Electron (v23.1.2) is packaged with Node (v18.21.1). According to the abi version found in [this link](https://github.com/mapbox/node-pre-gyp/blob/master/lib/util/abi_crosswalk.json), RobotJS currently only supports Node up to version 17.1.0 As a result, if you want to use RobotJS in your Electron project, you will need to downgrade your Electron version to a version which it's packaged Node version matches what can be found in the above link. See [this link](https://github.com/electron/releases) as well for reference. I'll be back in a 4 hours if you still need help. – midnight-coding Mar 10 '23 at 23:05

1 Answers1

0

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
midnight-coding
  • 2,857
  • 2
  • 17
  • 27
  • I followed the steps you mentioned but the problem still persists. It seems to be a general issue. I guess electron and robotjs cannot work together anymore. I have tried every possible solution but nothing seems to work... :( – olcaykuytak Mar 11 '23 at 14:37
  • Do you have 1. Python installed and 2. `node-gyp` installed? What versions of each of these are you running, noting that Python v2.7.3 is recommended and that Python v3.x.x is not supported. – midnight-coding Mar 11 '23 at 16:19
  • I downgraded my Python version to 2.7.3 and Electron version to 17.4.11, installed node-gyp and robots. I typed in the code you provided into the terminal, but it gave an error. I must be doing something wrong, but I can't figure out what it is. If you would like to connect to my computer and check, I would be happy to allow it. – olcaykuytak Mar 11 '23 at 19:17
  • When you type in the code provided at the CLI, is the CLI working directory set to the root of your project? IE: Open the CLI, change to the directory of your project (meaning the level where your `package.json` file is located; and then run the code I provided at the CLI. – midnight-coding Mar 11 '23 at 22:15
  • Even though I do everything as it should be, unfortunately it doesn't work out. I hope someone will make a tutorial video on how to integrate robotjs into an electronjs project and upload it to YouTube soon... :( – olcaykuytak Mar 12 '23 at 11:38
  • midnight-coding Have you tried creating and running a project yourself? I'm very curious. I think this is a common problem. – olcaykuytak Mar 13 '23 at 10:49