Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
johannesgiani opened this issue Oct 6, 2021 · 6 comments
Closed
3 tasks done
Labels
invalid Not related to Forge directly

Comments

@johannesgiani
Copy link

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"
                }
              ]
            }
          }
        ]
      ]
// ...
@johannesgiani

This comment has been minimized.

@malept malept added invalid Not related to Forge directly and removed bug labels Oct 6, 2021
@johannesgiani

This comment has been minimized.

@malept
Copy link
Member

malept commented 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.

@malept malept closed this as completed Oct 6, 2021
ouyangjunyi pushed a commit to ouyangjunyi/electron-react-webpack-typescript-2022 that referenced this issue Sep 13, 2022
@iCyris
Copy link

iCyris commented Dec 16, 2022

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.

@erickzhao
Copy link
Member

@iCyris: you can use ipcRenderer directly in the renderer process instead. No need for the context bridge. :)

@iCyris
Copy link

iCyris commented Dec 21, 2022

@erickzhao It works, thank u~ :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Not related to Forge directly
Projects
None yet
Development

No branches or pull requests

4 participants