Electron require ipcRenderer not working

Viewed 15595

I am trying to do a simple ipc.send and ipc.on but for some reason I am getting undefined on this electron require.

libs/custom-menu.js:

'use-strict';

const BrowserWindow = require('electron').BrowserWindow;
const ipcRenderer = require('electron').ipcRenderer;

exports.getTemplate = function () {
  const template = [
    {
      label: 'Roll20',
      submenu: [
        {
          label: 'Player Handbook',
          click() {
            console.log('test');
          },
        },
      ],
    },
    {
      label: 'View',
      submenu: [
        {
          label: 'Toggle Fullscreen',
          accelerator: 'F11',
          click(item, focusedWindow) {
            if (focusedWindow) {
              focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
            }
          },
        },
        {
          label: 'Toggle Developer Tools',
          accelerator: (function () {
            if (process.platform === 'darwin') {
              return 'Alt+Command+I';
            }
            return 'Ctrl+Shift+I';
          }()),
          click(item, focusedWindow) {
            if (focusedWindow) {
              focusedWindow.toggleDevTools();
            }
          },
        },
        {
          label: 'Reload',
          accelerator: 'F5',
          click() {
            BrowserWindow.getFocusedWindow().reloadIgnoringCache();
          },
        },
      ],
    },
    {
      label: 'Random Generators',
      submenu: [
        {
          label: 'World Generator',
          click() {
            ipcRenderer.send('show-world');
          },
        },
      ],
    },
  ];
  return template;
};

The error is cannot read property 'send' of undefined.

5 Answers

I know this answer might have been too late for you but for other

If you're trying access any of main process modules from renderer process you will need to go through remote module,

const {BrowserWindow} = require('electron').remote

see documentation remote

Just for those who can't get this to work in react app ipcRenderer or in any environment that requires preload file.

preload setup

These lines worked for me:

app.commandLine.appendSwitch('ignore-certificate-errors', 'true') 
app.commandLine.appendSwitch('allow-insecure-localhost', 'true')

In the renderer process, the script tags that have the "require" statement needs to be: <script type="javascript"></script>

Placing your call to require in a script tag without the type set doesn't work.

Related