forge icon indicating copy to clipboard operation
forge copied to clipboard

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

Open johannesgiani opened this issue 5 years ago • 6 comments

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

  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 avatar Oct 06 '21 13:10 johannesgiani

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

johannesgiani avatar Oct 06 '21 13:10 johannesgiani

Hey @malept can you explain why this is invalid? :)

johannesgiani avatar Oct 06 '21 14:10 johannesgiani

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 avatar Oct 06 '21 14:10 malept

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 avatar Dec 16 '22 07:12 iCyris

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

erickzhao avatar Dec 16 '22 17:12 erickzhao

@erickzhao It works, thank u~ :D

iCyris avatar Dec 21 '22 02:12 iCyris