Require('ipc') returns undefined in renderer process


#1

I am hosting a web-app in electron.exe, and am trying to send an IPC message from the renderer process to the main process, per code below:

    var ipc = require('ipc');
    ipc.send('close-main-window');

Problem is the “require(‘ipc’)” call always returns “undefined”.

I tried installing the IPC module (see cmd-line below), but that didn’t help:

C:>npm install ipc
ipc@0.0.1 node_modules\ipc
├── async@0.1.22
├── redis@0.7.3
└── winston@0.5.11 (stack-trace@0.0.9, colors@0.6.2, eyes@0.1.8, pkginfo@0.2.3,loggly@0.3.11)

I am new to the whole concept of Electron/Atom-shell, and probably am misunderstanding something very fundamental. Or maybe my paths are all messed up, due to my renderer script coming from a web-server, instead of being stored locally with the application.

Any help would be greatly appreciated!


#2

The IPC library is built-in to Electron, you don’t need to (and probably shouldn’t) install the ipc node module.

This also sounds like a likely culprit. Electron is designed primarily to easily build local applications using web technologies. It isn’t designed to be a replacement use-specific web browser.


#3

Thank you for the reply!

all I am doing is:
mainWindow.loadUrl(‘http://localhost/...some url’);

instead of:
mainWindow.loadUrl(‘file://C:/…some file’);

Moving the files from the server to the client would be problematic, as we would need to convert from cshtml to html, etc.

The page itself renders fine, and all the JS is working as expected. Only thing I seem to have trouble with is the require(‘ipc’) call.

Are you saying that it is impossible to render pages/scripts that are hosted on a server as opposed to stored locally? Or is there a trick I need to use?

Thanks again!


#4

You can host your files wherever but if wherever is outside of Electron (e.g. localhost) you won’t have access to Electron specific modules.

Maybe using a webview would work better in your case.


How to access the node api from a remote page in electron?
#5

I’m not saying it is impossible, no. I’m saying that isn’t what Electron is designed for. You can use a wrench to hammer in a nail, but that isn’t what the wrench was designed to do. Using a wrench that way once in a pinch is probably just fine … but relying on the wrench to be a hammer on a regular basis is probably going to bring up more and more issues over time.

@john phrased it better and with less metaphor :grinning:


#6

Thank you both!

I was hoping that there would be a way for me to specify the location of the module to load, or change the context of the app, so it would appear to be running locally. But it seems I am simply out of luck on this one.


#7

I’ve tracked the problem down to the node-integration switch:

if I create my browser window without it:
mainWindow = new BrowserWindow({width: 800, height: 600});
then require(‘ipc’); works as expected.

if however I create my browser window like this:
mainWindow = new BrowserWindow({width: 800, height: 600, “node-integration”: false});
then require(‘ipc’); blows up.

I am not sure what “node-integration”: false does exactly, but I had to add it in order for JQuery and everything else in my app to work.

Any insights would be greatly appreciated!


#8

Electron is based on top of Chromium and Node.js. Turning off Node integration in a window means that you can’t access any Node modules from that window … including all of the modules for the renderer process (which includes ipc).


#9

Argh! Thank you for clearing that up!

I guess now I have to figure out which of those third-party JS libraries in our page are interfering with Node.js…


#10

There is a topic here about integrating jQuery but the search is failing me.

explains the issue with jQuery and provides work arounds that don’t involve disabling node integration.


#11

Thanks John! I’ve read through them, but was unable to get my JQuery path to work properly.

My current (temporary) workaround is to hack the JQuery.js file directly, and remove the check for module being defined.

Maybe tomorrow with a clear head, everything will look obvious…