Robotjs: robot.js wont work with Electron

Created on 17 Mar 2019  Â·  25Comments  Â·  Source: octalmage/robotjs

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)

Most helpful comment

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)

All 25 comments

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)

Still can't get this to work.
an extremely long string of errors are occurring when running the last step using electron-rebuild. It seems to rebuild everything just fine until it encounters the robotjs module, and it errors out.
Any help would be appreciated.

I also encountered this problem.How do you solve this problem?

Bump?

I also encountered this problem.How do you solve this problem?
https://github.com/octalmage/robotjs/issues/466#issuecomment-475954595

Same here, I can't get it to compile. Please help.

https://pastebin.com/8pFLuz8g

yeh i have the same problem with electron rebuild

I ended up using xdotool instead, just launching it as a child process, if
anyone needed an alternative for now.

On Thu, Aug 15, 2019, 2:55 AM Yomi Adenaike notifications@github.com
wrote:

yeh i have the same problem with electron rebuild

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/octalmage/robotjs/issues/466?email_source=notifications&email_token=AA6OUC46ZY37V5NWHPHGX7TQEUKWLA5CNFSM4G7AWDE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4LH27A#issuecomment-521567612,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA6OUC43I2GAPV6DMGY6AUDQEUKWLANCNFSM4G7AWDEQ
.

Can that get the color of a pixel from the screen because that's all I need

It looks like it does not, just keyboard and mouse controls. One option that comes up in google is using xdotool with grabc which gets the pixel color under the mouse when the screen is clicked. Or you could probably take a screenshot with something and then use something else to get the pixel color at a location in the image. By drawing it to a canvas and then getting the color of a pixel for example, or finding a command line tool that can do it.

edit: assuming you're using linux. I'm not sure what windows tools you'd need to use. Feels weird recommending the use of something else in a project's github, but my first instinct was "ok damn let's try just using an old version of electron that will work with it" since I don't understand compiling errors. And I couldn't really get that to work either so then I realized duh just use a system alternative. So maybe it's helpful advice for other noobs like me.

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)

Which node modules folder? The one in the electron-rebuild module, or the one in the project folder?

The version we get from npm (0.5.1) just won't work as is. It has been released using apis that are now deprecated/deleted, or at least that is what I see in my console output when running rebuild. This should have been fixed with this commit but no new version has been pushed to npm since then.

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

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.

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

We are waiting please release soon it's causing a lot of problem in electron
Thanks for this great library

This new version has been published (0.6.0) but there is an error in the latest NodeJS version

@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

@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

Thanks, this worked. the npx electron-rebuild -f -t prod,optional,dev -w robotjs is key.

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!

$ "./node_modules/.bin/electron-rebuild" -w robotjs
â ´ Building module: robotjs, Completed: 0Attempting to build a module with a space in the path
See https://github.com/nodejs/node-gyp/issues/65#issuecomment-368820565 for reasons why this may not work
â ™ Building module: robotjs, Completed: 0gyp info find Python using Python version 3.8.5 found at "C:\Users\Jaagrav\AppData\Local\Programs\Python\Python38-32\python.exe"
â § Building module: robotjs, Completed: 0gyp ERR! find VS 
gyp ERR! find VS msvs_version not set from command line or npm config 
gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details      
gyp ERR! find VS looking for Visual Studio 2015
gyp ERR! find VS - not found
gyp ERR! find VS not looking for VS2013 as it is only supported up to 
Node.js 8
gyp ERR! find VS
gyp ERR! find VS **************************************************************
gyp ERR! find VS You need to install the latest version of Visual Studio
gyp ERR! find VS including the "Desktop development with C++" workload.
gyp ERR! find VS For more information consult the documentation at:   
gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows        
gyp ERR! find VS **************************************************************
gyp ERR! find VS
✖ Rebuild Failed

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'D:\Jaagrav's Stuff\Coding\Electron Projex\fidelity\node_modules\robotjs'.
Error: Could not find any Visual Studio installation to use



Error: node-gyp failed to rebuild 'D:\Jaagrav's Stuff\Coding\Electron 
Projex\fidelity\node_modules\robotjs'.
Error: Could not find any Visual Studio installation to use


    at ModuleRebuilder.rebuildNodeGypModule (D:\Jaagrav's Stuff\Coding\Electron Projex\fidelity\node_modules\electron-rebuild\lib\src\module-rebuilder.js:193:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Rebuilder.rebuildModuleAt (D:\Jaagrav's Stuff\Coding\Electron Projex\fidelity\node_modules\electron-rebuild\lib\src\rebuild.js:190:9)
    at async Rebuilder.rebuild (D:\Jaagrav's Stuff\Coding\Electron Projex\fidelity\node_modules\electron-rebuild\lib\src\rebuild.js:152:17) 
    at async D:\Jaagrav's Stuff\Coding\Electron Projex\fidelity\node_modules\electron-rebuild\lib\src\cli.js:146:9

Now what? Didn't work for me, I installed Visual Studio with everything required yet it did not work for some reason. Any alternate way?

Now what? Didn't work for me, I installed Visual Studio with everything required yet it did not work for some reason. Any alternate way?

@Jaagrav
well it just says install visual studio 2015 or newer (with "Desktop development with C++" selected. it's so important.).
if it didn't work, also try to set "VCINSTALLDIR" and "msvs_version" manually. (google for how to do it).

I already installed and figured out the problem. To those who are reading this, ignore the shit I wrote above, here's what worked for me on the first try.

In the docs, it clearly mentions this command to rebuild and compile robotjs and electronjs to the same version,

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

What I believe most people are doing wrong, perhaps the reason why this wasn't working for you as well is the --target flag in the command, see how it says 1.1.3 there? Well it's actually supposed to be the electron version that you're using, should've mentioned it in the docs...

So I ran

$ npx electron -v
v11.2.0 //My electron version

and the version was way ahead, I wonder why it wasn't working before. And immediately, literally immediately after I changed and executed this command,

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

Just like literal magic, it worked, in the first try! I spent so long trying to figure this out and the culprit was sitting right in front of my eyes!
However the funny thing is, the commands mentioned above by the other devs, which worked for them didn't seem to work for me and threw me the error that I put in my comment above.

THAT'S IT!

@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

@wikked1 im having simular issue, electron-rebuild build successful node_modules\robotjs\build\Release\robotjs.node

but when i try to use it in the electron i get an error:

ERROR in ./node_modules/robotjs/build/Release/robotjs.node
Module build failed (from ./node_modules/node-loader/dist/cjs.js):
TypeError: this.getOptions is not a function
    at Object.loader (D:\workspace\electron-app\node_modules\node-loader\dist\index.js:20:24)

any idea what could be?

"electron": "12.0.6",
"electron-rebuild": "^2.3.5",

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IChocolateKapa picture IChocolateKapa  Â·  12Comments

HopefulLlama picture HopefulLlama  Â·  3Comments

tms1337 picture tms1337  Â·  4Comments

Androiderr picture Androiderr  Â·  4Comments

ylh888 picture ylh888  Â·  3Comments