Upgraded Electron 5.0.1 It was found that the previous code could not run, and the front page reported an error: require is not defined Google searched and solved this problem.

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>

After saving, run Electron. The front page reports the following error:

solve the problem

In Electron5.0, the BrowserWindow's nodeIntegration Default is false In version 4. x, the default value is true If you pass in true in the construction parameter, you can use require.

Modify the corresponding part of main.js as follows:

 mainWindow = new BrowserWindow({ width:800, height:600, webPreferences: { nodeIntegration: true } });

Run after saving, and the console outputs normally Hello World

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/4845.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. Some time ago, I started learning Electron and wrote a local cartoon Picture reader, stepping on a pile of pits 😕