parro-it/electron-localshortcut

Allow using `electron-localshortcut` from render process

mitchhentges opened this issue · 11 comments

When I try to require('electron-localshortcut'), I get an error:

Uncaught TypeError: Cannot read property 'on' of undefined

This is because it's grabbing app by a direct require, while it needs to be obtained from remote if in the render process.

Though, I'm still getting the hang of this electron thing, and it might be a terrible idea to do this kind of thing from the render process. The more I play with this, the more it looks like it. ¯\_(ツ)_/¯

if you want to register shortcuts in renderer process, you can do

var localshortcut = require("remote").require("electron-localshortcut");

But as you said, it maybe preferable to do it in main process.

Ah, of course. That works gloriously. Thanks!

You're welcome!

I'm going to reopen this: after working with this some more, I've found some issues with this and browserify.

Module bundlers seem to look for require(x) statements, and then include x in the finished bundle. However, if a module is required through remote: require("remote").require("electron-localshortcut") then it isn't included in the final package.

Disclaimer: I haven't seen that many other people with this problem, so I'm potentially doing wrong.

You don't need to browserify you electron apps: electron platform is perfectly capable of loading modules at runtime using require.

If you want to browserify at any cost (e.g. because of module loading speed gain), then you have to search for a browserify transform that handle the remote case. Browserify only work with require calls, it does not know anything about remote calls.

I doubt that any such transform exists...

You can also give a shot to https://github.com/dominictarr/noderify

If I don't browserify, then my final package size shoots up from ~40MB to ~200MB (I package with the entire node_modules folder).

Than you have to try one of two solution explained above. Or you can also try to use webpack instead.
Anyway, I re-close this since it's not related to electron-localshortcut

👍, thanks for your help, it should be enough direction to continue :)

You're welcome!