allencoded
allencoded

Reputation: 7275

Electron require ipcRenderer not working

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.

Upvotes: 9

Views: 20521

Answers (5)

Dale Fernandes
Dale Fernandes

Reputation: 1

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.

Upvotes: -2

msdos
msdos

Reputation: 2529

These lines worked for me:

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

Upvotes: -1

praHoc
praHoc

Reputation: 367

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

preload setup

Upvotes: 0

Akumzy
Akumzy

Reputation: 43

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

Upvotes: 1

Vadim Macagon
Vadim Macagon

Reputation: 14847

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.

Upvotes: 7

Related Questions