Use node-usb inside electron


#1

Hi,

I am trying to use node-usb inside electron. Following the nativ modules docs i did this:

npm install electron-rebuild
npm install electron-prebuilt
npm install usb
./node_modules/.bin/electron-rebuild

which results in the following error:

electron-prebuilt@0.25.3 install app/node_modules/electron-prebuilt
node install.js

usb@1.0.6 install app/node_modules/usb
node-pre-gyp install --fallback-to-build

Unsupported target version: 0.25.3

node-pre-gyp ERR! install error
node-pre-gyp ERR! stack Error: Unsupported target version: 0.25.3
node-pre-gyp ERR! stack at get_runtime_abi (app/node_modules/usb/node_modules/node-pre-gyp/lib/util/versioning.js:140:23)
node-pre-gyp ERR! stack at Object.module.exports.evaluate (app/node_modules/usb/node_modules/node-pre-gyp/lib/util/versioning.js:249:19)
node-pre-gyp ERR! stack at install (app/node_modules/usb/node_modules/node-pre-gyp/lib/install.js:138:31)
node-pre-gyp ERR! stack at Object.self.commands.(anonymous function) [as install] (app/node_modules/usb/node_modules/node-pre-gyp/lib/node-pre-gyp.js:48:37)
node-pre-gyp ERR! stack at run (app/node_modules/usb/node_modules/node-pre-gyp/bin/node-pre-gyp:79:30)
node-pre-gyp ERR! stack at Object. (app/node_modules/usb/node_modules/node-pre-gyp/bin/node-pre-gyp:131:1)
node-pre-gyp ERR! stack at Module._compile (module.js:456:26)
node-pre-gyp ERR! stack at Object.Module._extensions…js (module.js:474:10)
node-pre-gyp ERR! stack at Module.load (module.js:356:32)
node-pre-gyp ERR! stack at Function.Module._load (module.js:312:12)
node-pre-gyp ERR! System Linux 3.19.3-200.fc21.x86_64
node-pre-gyp ERR! command “node” “app/node_modules/usb/node_modules/.bin/node-pre-gyp” “install” "–fallback-to-build"
node-pre-gyp ERR! cwd app/node_modules/usb
node-pre-gyp ERR! node -v v0.10.36
node-pre-gyp ERR! node-pre-gyp -v v0.6.4
node-pre-gyp ERR! not ok

npm ERR! Linux 3.19.3-200.fc21.x86_64
npm ERR! argv “node” “app/node_modules/electron-rebuild/node_modules/npm/bin/npm-cli.js” “rebuild” “–target=0.25.3” "–arch=x64"
npm ERR! node v0.10.36
npm ERR! npm v2.9.1
npm ERR! code ELIFECYCLE
npm ERR! usb@1.0.6 install: node-pre-gyp install --fallback-to-build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the usb@1.0.6 install script ‘node-pre-gyp install --fallback-to-build’.
npm ERR! This is most likely a problem with the usb package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-pre-gyp install --fallback-to-build
npm ERR! You can get their info via:
npm ERR! npm owner ls usb
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! app/npm-debug.log

Process exited with code: 1
Error: Process exited with code: 1
at ChildProcess. (app/node_modules/electron-rebuild/lib/spawn.js:53:26)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:766:16)
at Socket. (child_process.js:979:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:466:12)

Any pointers welcome to debug this, thanks :smile:


Struggling to get node-usb working in electron
#2

I’m having this same issue; hopefully one of us can find an answer!


Access to USB or Bluetooth?
#3

The following works for me; hopefully it will help someone else:

$ npm install --save usb@1.0.6
> usb@1.0.6 install /path/to/app/node_modules/usb
> node-pre-gyp install --fallback-to-build

[usb] Success: "/path/to/app/node_modules/usb/src/binding/usb_bindings.node" is installed via remote

Instead of using electron-rebuild (which I couldn’t get to work with node-usb’s use of node-pre-gyp), I used node-gyp directly:

$ npm install -g node-gyp

But first I needed to change the variables property in the node_modules/usb/binding.gyp to include module_name and module_path:

  'variables': {
    'use_udev%': 1,
    'use_system_libusb%': 'false',
    'module_name': 'usb_bindings',
    'module_path': './src/binding'
  },

Then, I rebuilt with node-gyp:

$ cd node_modules/usb
$ HOME=~/.electron-gyp node-gyp rebuild --target=0.26.0 --arch=ia64 --dist-url=https://atom.io/download/atom-shell
gyp info it worked if it ends with ok
gyp info using node-gyp@1.0.3
gyp info using node@0.12.0 | darwin | x64
gyp info spawn python
gyp info spawn args [ '/opt/boxen/nodenv/versions/v0.12.0/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/path/to/app/node_modules/usb/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/opt/boxen/nodenv/versions/v0.12.0/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/bruce/.electron-gyp/.node-gyp/0.26.0/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/bruce/.electron-gyp/.node-gyp/0.26.0',
gyp info spawn args   '-Dmodule_root_dir=/path/to/app/node_modules/usb',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/libusb/libusb/libusb/core.o
../libusb/libusb/core.c:2241:35: warning: comparison of integers of different signs: 'int' and 'unsigned long' [-Wsign-compare]
        if (header_len < 0 || header_len >= sizeof(buf)) {
                              ~~~~~~~~~~ ^  ~~~~~~~~~~~
../libusb/libusb/core.c:2250:44: warning: comparison of integers of different signs: 'int' and 'unsigned long' [-Wsign-compare]
        if (text_len < 0 || text_len + header_len >= sizeof(buf)) {
                            ~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~
2 warnings generated.
  CC(target) Release/obj.target/libusb/libusb/libusb/descriptor.o
  CC(target) Release/obj.target/libusb/libusb/libusb/hotplug.o
  CC(target) Release/obj.target/libusb/libusb/libusb/io.o
  CC(target) Release/obj.target/libusb/libusb/libusb/strerror.o
  CC(target) Release/obj.target/libusb/libusb/libusb/sync.o
  CC(target) Release/obj.target/libusb/libusb/libusb/os/poll_posix.o
  CC(target) Release/obj.target/libusb/libusb/libusb/os/threads_posix.o
  CC(target) Release/obj.target/libusb/libusb/libusb/os/darwin_usb.o
../libusb/libusb/os/darwin_usb.c:1281:27: warning: expression which evaluates to zero treated as a null pointer constant of type
      'IOUSBInterfaceInterface550 **' (aka 'struct IOUSBInterfaceStruct550 **') [-Wnon-literal-null-conversion]
  cInterface->interface = IO_OBJECT_NULL;
                          ^~~~~~~~~~~~~~
/System/Library/Frameworks/IOKit.framework/Headers/IOTypes.h:164:24: note: expanded from macro 'IO_OBJECT_NULL'
#define IO_OBJECT_NULL  ((io_object_t) 0)
                        ^~~~~~~~~~~~~~~~~
1 warning generated.
  LIBTOOL-STATIC Release/usb.a
  CXX(target) Release/obj.target/usb_bindings/src/node_usb.o
  CXX(target) Release/obj.target/usb_bindings/src/device.o
  CXX(target) Release/obj.target/usb_bindings/src/transfer.o
  SOLINK_MODULE(target) Release/usb_bindings.node
  SOLINK_MODULE(target) Release/usb_bindings.node: Finished
  COPY ../src/binding/usb_bindings.node
  TOUCH Release/obj.target/action_after_build.stamp
gyp info ok

After the rebuild, I’m able to use the library from my Electron main.js just fine.


#4

Thanks for this! One small note for anyone who finds this in the future:

The --target flag for the last command specifies the version of electron you’re building for and must be set accordingly.

You can get your version by typing the command electron -v.


Electron error after running node-gyp rebuild
#5

Thank you very much bruce!!

On windows 7 (64bits) I followed all the steps like you did, but to rebuild I did:

cd node_modules/usb
node-gyp rebuild --target=0.30.1 --dist-url=https://atom.io/download/atom-shell

Struggling to get node-usb working in electron
#6

On Ubuntu, you may find an error like

no such option --no-parallel

Following this easy step you can solve it: http://stackoverflow.com/questions/22767121/install-ctags-from-npm-no-such-option-no-parallel/23342260#23342260


#7

On raspberry pi 2 you may need to update GCC.

https://solarianprogrammer.com/2015/01/13/raspberry-pi-raspbian-install-gcc-compile-cpp-14-programs/


#8

I’m following these steps and keep hitting a wall on the last step, I was hoping someone could help me out. I complete bruce’s process above by issuing

node-gyp rebuild --target=0.33.2 --dist-url=https://atom.io/download/atom-shell

and this command completes ending with

gyp info ok

After this step, I’m pretty sure that I need to rebuild electron using

./node_modules/.bin/electron-rebuild

However, when I do that, I get the following error dump:

Unknown Runtime: 'electron'
node-pre-gyp ERR! install error
node-pre-gyp ERR! stack Error: Unknown Runtime: 'electron'
node-pre-gyp ERR! stack     at get_runtime_abi (C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\usb\node_
modules\node-pre-gyp\lib\util\versioning.js:60:19)
node-pre-gyp ERR! stack     at Object.module.exports.evaluate (C:\Users\Jonathan\Documents\electron-for-hardware\node_mo
dules\usb\node_modules\node-pre-gyp\lib\util\versioning.js:249:19)
node-pre-gyp ERR! stack     at install (C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\usb\node_modules\
node-pre-gyp\lib\install.js:138:31)
node-pre-gyp ERR! stack     at Object.self.commands.(anonymous function) [as install] (C:\Users\Jonathan\Documents\elect
ron-for-hardware\node_modules\usb\node_modules\node-pre-gyp\lib\node-pre-gyp.js:48:37)
node-pre-gyp ERR! stack     at run (C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\usb\node_modules\node
-pre-gyp\bin\node-pre-gyp:79:30)
node-pre-gyp ERR! stack     at Object. (C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\usb\no
de_modules\node-pre-gyp\bin\node-pre-gyp:131:1)
node-pre-gyp ERR! stack     at Module._compile (module.js:460:26)
node-pre-gyp ERR! stack     at Object.Module._extensions..js (module.js:478:10)
node-pre-gyp ERR! stack     at Module.load (module.js:355:32)
node-pre-gyp ERR! stack     at Function.Module._load (module.js:310:12)
node-pre-gyp ERR! System Windows_NT 6.3.9600
node-pre-gyp ERR! command "node" "C:\\Users\\Jonathan\\Documents\\electron-for-hardware\\node_modules\\usb\\node_modules
\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\usb
node-pre-gyp ERR! node -v v0.12.7
node-pre-gyp ERR! node-pre-gyp -v v0.6.4
node-pre-gyp ERR! not ok

npm ERR! Windows_NT 6.3.9600
npm ERR! argv "node" "C:\\Users\\Jonathan\\Documents\\electron-for-hardware\\node_modules\\electron-rebuild\\node_module
s\\npm\\bin\\npm-cli.js" "rebuild" "--runtime=electron" "--target=0.32.3" "--arch=ia32"
npm ERR! node v0.12.7
npm ERR! npm  v2.14.8
npm ERR! code ELIFECYCLE
npm ERR! usb@1.0.6 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the usb@1.0.6 install script 'node-pre-gyp install --fallback-to-build'.
npm ERR! This is most likely a problem with the usb package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-pre-gyp install --fallback-to-build
npm ERR! You can get their info via:
npm ERR!     npm owner ls usb
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\npm-debug.log

Process exited with code: 1
Error: Process exited with code: 1
    at ChildProcess. (C:\Users\Jonathan\Documents\electron-for-hardware\node_modules\electron-rebuild\lib\spa
wn.js:55:26)
    at ChildProcess.emit (events.js:110:17)
    at maybeClose (child_process.js:1015:16)
    at Process.ChildProcess._handle.onexit (child_process.js:1087:5)

Any idea why I’m failing at this point? I’ve already been able to install other native modules and rebuild successfully into my electron app without any problems (module: usb-detection), so I’m not sure what I’m missing here.

Node version 0.12.7
npm version 2.11.3
Python 2.7.3
Windows 8.1

Any insight would be very much appreciated. Thanks in advance.


#9

Does it help if you provide a --version argument like electron-rebuild --version v0.33.7?

https://ci.appveyor.com/project/kevinmehall/node-usb/branch/master shows that the CI is only run on node versions up to 1.6.3 (latest Electron is currently using node 4.x). Maybe you just need to use an Electron that is in sync with the node version compatible with node-usb. From the releases page it looks like v0.23.0 was using node 1.6.x.


#10

Thanks for the tips. I tried the --version argument and it didn’t make any difference in the outcome, still got the same error. I’ve been experimenting with different versions of node.js as well but haven’t had any luck as of yet. I’m not ready to give up though, so I’ll keep trying to get my configuration just right. Do you know if there’s any value in trying to build with versions of io.js instead of node.js? I’m a real new-comer to this so I don’t know if that would make any difference or not.


#11

Hello, Bruce
Could you please detail that process, I had the same issues by using the latest versions of NPM , NODEJS and ELECTRONJS.

Could you please tell me about the versions of the software used to rebuild it with USB enabled ?

did you consider the following :


or

thank you in advance.


#12

Hey @bruce , I followed this solution of yours and I solved the problem of prebuilt-binary but when I am using the code in main.js. The electorn is giving error of mismatch version. Can you help me? I would be grateful to you.

App threw an error when running [Error: Module version mismatch. Expected 47, got 57.]
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Module version mismatch. Expected 47, got 57.
    at Error (native)
    at process.module.(anonymous function) [as dlopen] (ATOM_SHELL_ASAR.js:159:20)
    at Object.Module._extensions..node (module.js:450:18)
    at Object.module.(anonymous function) [as .node] (ATOM_SHELL_ASAR.js:159:20)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/home/lalit/Desktop/Github/Electron/simple-electron/simple-electron-react/node_modules/usb/usb.js:5:38)
    at Module._compile (module.js:425:26)
  • I am using this code in my main.js
const usb = require('usb');
usb.on('attach', function(device) {
  console.log(device);
});

Please tell me, if I am missing something.