前提
本文所描述的问题,出现在渲染进程中。如果您的相关错误,是在主进程中出现的,那么并不适合本文的操作。
问题描述
跟随官网的实例,再进行主进程到渲染进程通信的时候,出现了错误
官网代码
//在渲染器进程 (网页) 中。
const { ipcRenderer } = require('electron')
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"
ipcRenderer.on('asynchronous-reply', (event, arg) => {
console.log(arg) // prints "pong"
})
ipcRenderer.send('asynchronous-message', 'ping')
系统报错Uncaught ReferenceError: require is not defined
解决方案
开启BrowserWindow
的nodeIntegration: true
mainWindow = new BrowserWindow({
//...
webPreferences: {
nodeIntegration: true
}
})
最新的electron@5.0系列中,这个nodeIntegration参数,默认改成false了。而在以前版本的electron中,这个nodeIntegration参数,默认为true。