electron: Uncaught ReferenceError: require is not defined

  • Electron Version: from 5.0.0-beta.5 to 5.0.0-beta.1
  • Operating System: macOS 10.14.3
  • Last Known Working Electron version:: 4.0.7

Problem

Build and pack passed well, but after run app, I see an error in console Uncaught ReferenceError: require is not defined - at Object.electron - at __webpack_require__.

Screenshots

screenshot 2019-03-06 at 15 20 05

Additional Information

We use Typescript, electron-builder, webpack.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 19 (2 by maintainers)

Commits related to this issue

Most upvoted comments

in your index.js

const w = new BrowserWindow({ webPreferences: { nodeIntegration: true } })

actually i added nodeIntergration = true but dosent work still

Assuming electron 12.0.0

set contextIsolation: false

Something like…

webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    }

reference: https://stackoverflow.com/a/66455544/4097181

in your index.js

const w = new BrowserWindow({ webPreferences: { nodeIntegration: true } })

Thanks! Have solved my problem.

👋 Thanks for opening your first issue here! If you’re reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

The default for nodeIntegration changed from true to false as per our breaking changes document.

@brntsllvn Adding

webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    }

Works like a charm!! Thing is, do these other properties affect your application in any way?

Regards,

Need to change webPreferences.nodeIntegration to true when new BrowserWindow mainWindow = new BrowserWindow({ width: 1000, height: 600, webPreferences: { nodeIntegration: true } });

It didn’t work for me because webPreferences.contextIsolation was set to true. Be careful when setting that option to false though https://www.electronjs.org/docs/tutorial/context-isolation#security-considerations.

setting nodeIntegration to true is the easy way, bu keeping it false is more secure as documentation tells, I ve found this https://stackoverflow.com/questions/52236641/electron-ipc-and-nodeintegration that may help future readers.