This “require is not defined” is pretty annoying to me as Electron user.
Last year when I got this problem, I can simply solve it by adding: nodeIntegration: true.
But this time, on 2021, this problem comes again. So to solve it we need to add another thing here: contextIsolation: false.
I don’t know is it going to be a new game next year or what.
So, right now my main.js file to make my Electron program running is this:
const { ipcMain, app, BrowserWindow } = require('electron');
function createWindow () {
const win = new BrowserWindow({
width: 1280,
height: 720,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
})
win.loadFile('index.html');
win.webContents.openDevTools();
win.removeMenu();
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
ipcMain.on('quitprogram', (evt, arg) => {
app.quit();
});