question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught ReferenceError: require is not defined with node integration & webpack

See original GitHub issue

Pre-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

  1. npx create-electron-app node-integration --template=webpack
  2. cd node-integration
  3. enable node integration in forge config / package.json and browser window / main.js (see package.json and main.js below)
  4. npm start
  5. 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:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maleptcommented, Oct 6, 2021

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.

0reactions
iCyriscommented, Dec 21, 2022

@erickzhao It works, thank u~ 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found