Solve the problem of error reported by Electron 5. x require is not defined
Error code example
Back end main.js
const{app,ipcMain,BrowserWindow } = require("electron"); let mainWindow; function CreateWindow(){ mainWindow = new BrowserWindow({ width:800, height:600 }); mainWindow.loadFile("./html/index.html"); } ipcMain.on("Test",(sender,args)=>{ console.log(args); }) app.on("ready",()=>{ CreateWindow(); });
Front index.html
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script> const {ipcRenderer} = require("electron"); </script> </head> <body> <script> ipcRenderer.send("Test","Hello World"); </script> </body> </html>
solve the problem
mainWindow = new BrowserWindow({ width:800, height:600, webPreferences: { nodeIntegration: true } });