Uncaught ReferenceError: require is not defined with node integration & webpack
Pre-flight checklist
- [X] I have read the contribution documentation for this project.
- [X] I agree to follow the code of conduct that this project uses.
- [X] I have searched the issue tracker for a bug that matches the one I want to file, without success.
Electron Forge version
6.0.0-beta.61
Electron version
v15.1.1
Operating system
macOS 11.3
Last known working Electron Forge version
No response
Expected behavior
Nothing should be logged. (except CSP warning)
Actual behavior
renderer console says:
Uncaught ReferenceError: require is not defined
at Object.url (index.js:376)
at __webpack_require__ (index.js:405)
at fn (index.js:592)
at eval (parseURL.js:5)
at Module../node_modules/webpack-dev-server/client/utils/parseURL.js (index.js:193)
at __webpack_require__ (index.js:405)
at fn (index.js:592)
at eval (index.js?protocol=ws%3A&hostname=0.0.0.0&port=3000&pathname=%2Fws&logging=info:7)
at Module../node_modules/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&port=3000&pathname=%2Fws&logging=info (index.js:107)
at __webpack_require__ (index.js:405)
Steps to reproduce
npx create-electron-app node-integration --template=webpackcd node-integration- enable node integration in forge config / package.json and browser window / main.js (see
package.jsonandmain.jsbelow) npm start- Open dev tools / console
Additional information
main.js
// ...
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
// ...
package.json
// ...
"plugins": [
[
"@electron-forge/plugin-webpack",
{
"mainConfig": "./webpack.main.config.js",
"renderer": {
"nodeIntegration": true,
"config": "./webpack.renderer.config.js",
"entryPoints": [
{
"html": "./src/index.html",
"js": "./src/renderer.js",
"name": "main_window"
}
]
}
}
]
]
// ...
This seems to be related to https://github.com/electron-userland/electron-forge/issues/2284 and the omnibus ticket https://github.com/electron-userland/electron-forge/issues/2289
Hey @malept can you explain why this is invalid? :)
Thanks for the bug report but this is not a bug in Electron Forge.
Starting from Electron v12.0.0, the default value of contextIsolation changed from false to true, which prevents use of require in the renderer even if nodeIntegration is enabled. Primarily for security reasons, instead of using require in the renderer, it's recommended that you instead use contextBridge in a preload script to provide functions to the renderer process instead of using require.
If you need more assistance, please use one of the community forums, including the Electron Discord server.
After set nodeIntegration: true and contextIsolation: false, the require failure issue is fixed, but i can't use ipcRenderer & ipcMain any more, it consoles:
Error: contextBridge API can only be used when contextIsolation is enabled.
@malept Is there any way to use ipcRenderer & ipcMain when set nodeIntegration: true? Thanke you.
@iCyris: you can use ipcRenderer directly in the renderer process instead. No need for the context bridge. :)
@erickzhao It works, thank u~ :D