electron 5.0.0 "Uncaught ReferenceError: require is not defined"

Solution 1:

For Electron version 12 and above

const electron = require("electron");

const { app, BrowserWindow } = electron;

app.on("ready", () => {
  const mainWindow = new BrowserWindow({
    width: 1000,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    },
  });
  mainWindow.loadURL(`file://${__dirname}/index.html`);
});

Solution 2:

It turns out, nodeIntegration was true by default in previous electron versions, but false by default in 5.0.0. Consequently, setting it to true resolved my issue. Not finding this change documented online in comments or on electrons page, I thought I'd make this self-answered SO post to make it easier to find for future people who encounter this issue.

Solution 3:

Like junvar said, nodeIntegration is now false by default in 5.0.0.

The electronjs FAQ has some sample code on how to set this value.

let win = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
})
win.show()