Sometimes, we want to fix Electron require() is not defined with JavaScript.
In this article, we’ll look at how to fix Electron require() is not defined with JavaScript.
How to fix Electron require() is not defined with JavaScript?
To fix Electron require() is not defined with JavaScript, we enable Node integration.
For instance, we write
app.on("ready", () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
});
to open a new BrowserWindow
when the app is loaded.
We set nodeIntegration
to true
to enable Node.js integration, which makes the require
function available in our code.
Conclusion
To fix Electron require() is not defined with JavaScript, we enable Node integration.