Uncaught ReferenceError: require is not defined with node integration & webpack
See original GitHub issuePre-flight checklist
- I have read the contribution documentation for this project.
- I agree to follow the code of conduct that this project uses.
- 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=webpack
cd node-integration
- enable node integration in forge config / package.json and browser window / main.js (see
package.json
andmain.js
below) 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"
}
]
}
}
]
]
// ...
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
webpack : Uncaught ReferenceError: require is not defined
in web app. According to https://github.com/liady/webpack-node-externals it is only for backend. Since you use nodeExternals in web app ...
Read more >require is not defined with node integration & webpack · Issue ...
Uncaught ReferenceError : require is not defined at Object.url (index.js:376) at __webpack_require__ (index.js:405) at fn (index.js:592) at ...
Read more >How To Fix ReferenceError require is not defined in JavaScript
You are using require in a Node.js environment. In this case, check your package.json file for an property called type . If that...
Read more >Fix for require is not defined : r/node - Reddit
Trying to use the openai npm package when I get the error. Uncaught ReferenceError: require is not defined. when I try to do...
Read more >index.js:1 uncaught referenceerror: require is not defined
This is because require() is not a native browser function and is only supported in Node.js. To resolve this error, you can use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 fromfalse
totrue
, which prevents use ofrequire
in the renderer even ifnodeIntegration
is enabled. Primarily for security reasons, instead of usingrequire
in the renderer, it’s recommended that you instead usecontextBridge
in a preload script to provide functions to the renderer process instead of usingrequire
.If you need more assistance, please use one of the community forums, including the Electron Discord server.
@erickzhao It works, thank u~ 😄