Electron开发过程中,在index.html使用 require('./renderer.js')时报错require is not defined。
原因在于较新版本中nodeIntegration和contextIsolation的默认配置发生了变化,恢复其配置即可。
app.whenReady().then(() => {
const mainWindow = new BrowserWindow({
height: 600,
width: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
})
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools()
})
即上面webPreferences中的两个参数,新版本中二者缺一不可。
棒!