Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron require ipcRenderer not working

Tags:

electron

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.

like image 685
allencoded Avatar asked Mar 05 '16 09:03

allencoded


1 Answers

The BrowserWindow module is only available in the main process, the ipcRenderer module is only available in the renderer process, so regardless of which process you run this code in it ain't gonna work. I'm guessing since ipcRenderer is not available you're attempting to run this code in the main process.

like image 177
Vadim Macagon Avatar answered Sep 29 '22 20:09

Vadim Macagon