robotjs: robot.js wont work with Electron

Expected Behavior

App runs and starts logging mouse position

Current Behavior

App doesnt run and spits out this error:

Uncaught Error: The module '/Users/gingkathfox/Documents/GitHub/Bugwars/node_modules/robotjs/build/Release/robotjs.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 69. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:722)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
    at Module.load (internal/modules/cjs/loader.js:602)
    at tryModuleLoad (internal/modules/cjs/loader.js:541)
    at Function.Module._load (internal/modules/cjs/loader.js:533)
    at Module.require (internal/modules/cjs/loader.js:640)
    at require (internal/modules/cjs/helpers.js:20)
    at Object.<anonymous> (/Users/gingkathfox/Documents/GitHub/Bugwars/node_modules/robotjs/index.js:1)
    at Object.<anonymous> (/Users/gingkathfox/Documents/GitHub/Bugwars/node_modules/robotjs/index.js:38)

Possible Solution

Steps to Reproduce (for bugs)

I cant include much code (private), but here goes:

  1. Install robotjs, Electron, and CraftyJS
  2. Run this code through Electron:
 const robot = require('robotjs')
 const Crafty = require('craftyjs')
 Crafty.init(500,500)
 Crafty.background('black')
 Crafty.e('2D, Canvas, Color')
          .attr({
            x: 20,
            y: 200,
            w: 10,
            h: 10
          })
          .color('green')
          .bind('UpdateFrame', function() {
            console.log(robot.getMousePos())
          })
  1. Watch the error get spit out.

Context

I need to grab the current mouse position to fire a bullet towards it.

Your Environment

  • RobotJS version: 0.5.1
  • Node.js version: 10.15.3
  • npm version: 6.4.1
  • Operating System: darwin (macOS)

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 4
  • Comments: 29

Most upvoted comments

@mike-clark-8192 - worked perfectly for me, thanks! 😃

npm i robotjs
npm i -D electron-rebuild
npm install -g node-gyp
npx electron-rebuild -f -t prod,optional,dev -w robotjs

Try rebuilding robotjs with electron-rebuild:

Step 1: Follow instructions at https://github.com/octalmage/robotjs#building The last step you need from those instructions is npm install -g node-gyp.

Step 2:

npm install -D electron-rebuild
npx electron-rebuild -f -t prod,optional,dev -w robotjs

If you can get these commands to run without error in your project root (next to your root package.json), it should build you a version of robotjs that works with your Electron. Note that this won’t get you all binaries for all platforms, only the platform on which you run the command.

If you’re using a version of node+npm that doesn’t have npx, or have some other problem, try reading the instructions for alternate ways of running electron-rebuild here: https://github.com/electron/electron-rebuild

Note 1: Alternate invocation: npx electron-rebuild -f -m node_modules/robotjs

Note 2: This has not been tested with ABI > 72, meaning versions of Electron that contain Node v13+. AFAIK there is no stable release of Electron using Node v13+ though.

Did you follow the electron installation instruction ? https://github.com/octalmage/robotjs/wiki/Electron If not this should work:

  1. Require all dependencies you need in the main.js file that is run by electron. (this seemed to be the first important part for me)
  2. Run npm i -D electron-rebuild to add the electron-rebuild package
  3. Remove the node-modules folder, as well as the packages-lock.json file.
  4. Run npm i to install all modules.
  5. Run ./node_modules/.bin/electron-rebuild to rebuild everything (from https://stackoverflow.com/questions/46384591/node-was-compiled-against-a-different-node-js-version-using-node-module-versio)

a new version of robotjs supporting the latest NodeJS version will be published soon.

@Jaagrav you’re right you can usually get it working that way if you can figure out your nodejs version, electron version, and ABI version. But why go through the trouble when the electron-rebuild tool is available specifically to make this exact problem easier? Also electron-rebuild can help compile other types of native projects where the exact arguments to rebuild are less clear. It’s just a better approach, IMO.

npm i robotjs
npm i -D electron-rebuild
npm install -g node-gyp
npx electron-rebuild -f -t prod,optional,dev -w robotjs

https://github.com/octalmage/robotjs/issues/466#issuecomment-590491327

This worked for me:

  1. remove package-lock.json and nodemodules folder
  2. run npm i
  3. run "./node_modules/.bin/electron-rebuild" -w robotjs

versions: “electron”: “^10.1.3”, “robotjs”: “^0.6.0” “electron-rebuild”: “^2.2.0” node v10.18.0

@ya3ya6 worked for me too! thanks!