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:
- Install robotjs, Electron, and CraftyJS
- 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())
})
- 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
@mike-clark-8192 - worked perfectly for me, thanks! đ
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:
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/robotjsNote 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:
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.
https://github.com/octalmage/robotjs/issues/466#issuecomment-590491327
This worked for me:
package-lock.jsonandnodemodulesfoldernpm i"./node_modules/.bin/electron-rebuild" -w robotjsversions: âelectronâ: â^10.1.3â, ârobotjsâ: â^0.6.0â âelectron-rebuildâ: â^2.2.0â node v10.18.0
@ya3ya6 worked for me too! thanks!