Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron ipcMain undefined

The following code produces an error

const ipcMain = require('electron').ipcMain;
ipcMain.on('open-file-dialog', function (event) {});

This error is thrown in console:

Uncaught TypeError: Cannot read property 'on' of undefined

As mentioned on this question, i also tried using

const ipcMain = require('ipc-main');

but am getting the same error.

Seems that ipcRenderer is defined in the electron package, but not ipcMain. How do i fix this? Already tried reinstalling the latest nodejs and running npm install on a fresh checkout.

like image 475
Tom Avatar asked May 15 '17 06:05

Tom


2 Answers

In Renderer process you should use the counterpart of ipcMain, which is ipcRenderer. See docs of ipcMain for code examples

Your corrected code would look like

const { ipcRenderer } = require('electron');
ipcRenderer.on('open-file-dialog', function (event) {});
like image 153
pergy Avatar answered Sep 19 '22 00:09

pergy


Problem seems to be that I loaded the module from a renderer process. Moving the ipcMain related code to the main module (unsurprisingly) solved the issue.

like image 32
Tom Avatar answered Sep 20 '22 00:09

Tom