iohook: Error: Cannot find module 'iohook'

Hi, I have created an electron app that uses the ioHook library in it, as soon as I pack the app running (using yarn run build) and try to open the packaged app (both in mac and windows) the app crashes claiming it can’t find the module.

If I run the app using yarn start or electron . it works perfectly!

Expected Behavior

I would’ve expected it to get compiled with the rest of the libraries I use.

Current Behavior

Currently, the app crashes saying “Uncaught exception: Error: Cannot find module ‘ioHook’” and then the stack trace.

Possible Solution

Not sure, wish I had 😃

Steps to Reproduce (for bugs)

  1. I added the library using yarn add iohook
  2. imported the library using const ioHook = require('iohook');
  3. Called it in my code and used the relevant event listeners.
  4. Tried to wrap it as a packaged app and run it

Context

I am trying to get the library running as part of an electron app without the need to install the library nowhere externally.

Your Environment

  • Version used: 1.7.0 (since this is the newest version I saw it is supported)
  • Environment name and version (e.g. Chrome 39, node.js 5.4): NodeJS 9.2.0 (since this is the newest version I saw it is supported)
  • Operating System and version (desktop or mobile): MacOS (High Sierra) and Windows 10 x64
  • Link to your project: It’s a private repo

Any ideas? Hope I gave enough info!

Thanks, Eyal

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 54 (3 by maintainers)

Most upvoted comments

Hm, it’s simple to solve.

For example, if you have error like this

Error: Cannot find module '../node_modules/iohook/builds/electron-v75-darwin-x64/build/Release/iohook.node'

See part of error message electron-v75-darwin-x64, put their to package.json

  "iohook": {
    "targets": [
      "electron-75"
    ],
    "platforms": [
      "darwin"
    ],
    "arches": [
      "x64"
    ]
  }

and install iohook again

npm i -S iohook

That it’s.

Work for me.

A note to anyone installing with Yarn: be sure to remove iohook from your global cache before reinstalling or you won’t end up with the builds for the environments that you’ve added.

yarn cache clean iohook

@zxc23 thank you for your reply, I was able to find the solution, I was using the latest electron version, but it seems, for now, it is only supported on 8.x.x, this comment here explains basically everything you need to do:

https://github.com/wilix-team/iohook/issues/241#issuecomment-652517691

Hi @Djiit ,

I actually referred to the yarn run build command which packs the app. I tried now to get rid of the --asar flag from the electron-packager command so the library won’t make an archive file from the source code. that actually solved the problem! and my app works just fine. BUT! I do want to “hide” my code inside an archive file. so I assume once the app is in an archive, it can’t find the ioHook library.

Any ideas? I am digging into it now and will come back with updates.

Hi Djiit! Thanks so much for the quick reply. yeah I def did it and actually added all the electron versions available since I get an error when I don’t do it. my ioHook config in my package.json looks like this:

"iohook": {
    "targets": [
      "node-59",
      "electron-47",
      "electron-54",
      "electron-57"
    ],
    "platforms": [
      "win32",
      "darwin"
    ],
    "arches": [
      "x64",
      "ia32"
    ]
  }

Then, I am running yarn add ioHook to re-fetch the binaries.

But, still as soon as I try to compile the app and run the result - I get the same error that the ioHook library is missing.

Hi there,

Did you add the ‘iohook’ config in your package.json ? According to your env, i’ll say :

"iohook": {
  "targets": [
    "node-59",
    "electron-54"
  ],
  "platforms": [
    "win32",
    "darwin"
  ],
  "arches": [
    "x64",
    "ia32"
  ]
}

And then re-download your dependencies; this way it will fetch prebuild versions of iohook for node (the one you use in development and when using electron . AND for electron (the one you are supposed to use in production.