How to install node-hid for electron

Viewed 1274

I'm trying to get a simple electron application running that interacts with an HID device. I am running into a lot of difficulty, I am able to install and run node-hid, just not within electron.

My package.json looks like this:

{
    "name": "test-proj",
    "version": "1.0.0",
    "description": "",
    "main": "main.js",
    "scripts": {
        "start": "electron .",
        "rebuild": "electron-rebuild --force",
        "postinstall": "electron-rebuild"
    },
    "author": "me",
    "license": "ISC",
    "devDependencies": {
        "electron": "^9.2.0",
        "electron-rebuild": "^1.11.0"
    },
    "dependencies": {
        "node-hid": "^1.3.0"
    }
}

My main.js has the standard electron demo boilerplate code plus some HID calls:

const {app, BrowserWindow, ipcMain} = require('electron');
var HID = require('node-hid');
var device = new HID.HID(5824, 1500);
// Standard electron app window stuff

When trying to run the app I immediately get an error on new HID.HID():

Error: Module did not self-register: '\\?\C:\Users\Nathan\Desktop\carriage_return_app\node_modules\node-hid\build\Release\HID.node'.
    at process.func [as dlopen] (electron/js2c/asar.js:140:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1034:18)
    at Object.func [as .node] (electron/js2c/asar.js:140:31)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module._load (electron/js2c/asar.js:769:28)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at bindings (C:\Users\Nathan\Desktop\carriage_return_app\node_modules\bindings\bindings.js:112:48)
    at loadBinding (C:\Users\Nathan\Desktop\carriage_return_app\node_modules\node-hid\nodehid.js:25:42)

Looking around I have seen a few examples of how to install node-hid and use it in electron, but none have helped me.

I have followed the electron-rebuild instructions here: https://www.electronjs.org/docs/tutorial/using-native-node-modules

I have also followed the instructions and copied the examples from here: https://github.com/node-hid/node-hid#electron-projects-using-node-hid

No luck. I get module did not self-register every time. node-hid is supposed to support electron and I don't see other people complaining. Anyone come across this or have ideas to fix the issue?

1 Answers

The problem has been solved, so I will mark this as answered. I think I would still like more information on WHY though.

The hint came from building the two example projects:

Running the first item, electron-hid-test worked off the bat. Adding electron-builder as a dependency and adding "postinstall": "electron-builder install-app-deps" as a script solved the issue in my project, I don't want electron-builder yet but at least it indicated that node-hid can work with electron given a specific setup.

The second item also worked, but not with:

  1. npm install
  2. npm rebuild
  3. npm start

Instead it was specifically required to add the run command to the rebuild command:

  1. npm install
  2. npm run rebuild
  3. npm start

After that, it works. Any insights as to why this behaves differently?

Related