Simple IPC Clarification Help!


Just learning Electron, doing this fun Tutorial
I realized quickly that this was written in an earlier version of Electron, but I’m now on the IPC section (Closing the application from a browser window via remote events), and I’m a bit confused.

The tutorial says:
Add the following to main.js to subscribe to a channel:
var ipc = require(‘ipc’);

ipc.on(‘close-main-window’, function () {

To send a message on that channel, add the following to index.js:
var ipc = require(‘ipc’);

var closeEl = document.querySelector(’.close’);
closeEl.addEventListener(‘click’, function () {

Simple enough, but I know that the IPC mechanisms have changed in 1.0. I assumed that in main.js I’d use:
const {ipcMain} = require(‘electron’);
ipcMain.on(‘close-main-window’, function(){

and in index.js I’d use:
const {ipcRenderer} = require(‘electron’);
var closeEl = document.querySelector(’.close’);
closeEl.addEventListener(‘click’, function() {

BUT, all that happens is an exception is thrown - Uncaught Exception: Reference Error: ap is not defined.

Any help/clarification/information would be amazing! Thanks for the help.


I’m going to assume you meant to write app in your code and not “ap”. You will need to define app with the line
const {app} = require(electron);


No, it was “ap”. I do have the const electron app start - everything works awesome, just not the eventing - when I click on the close which triggers the event, I get the exception. It’s an event exception.


I’m a total idiot. Never mind. In updating the code to run on the latest Electron, I changed the names of the variables of course, but then didn’t change the variables in the methods - ap is, of course, the old variable name from the original tutorial. My new variable was const {app} = electron;

Sorry. Ugh. Nothing to see here.


Good point for the tutorial link. Thans to you !