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 #17241

Closed
iKBAHT opened this issue Mar 6, 2019 · 19 comments
Closed

Uncaught ReferenceError: require is not defined #17241

iKBAHT opened this issue Mar 6, 2019 · 19 comments

Comments

@iKBAHT
Copy link

iKBAHT commented Mar 6, 2019

  • 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.

@welcome
Copy link

welcome bot commented Mar 6, 2019

👋 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.

@MarshallOfSound
Copy link
Member

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

@iKBAHT
Copy link
Author

iKBAHT commented Mar 6, 2019

How can I find the breaking changes document?

@MarshallOfSound
Copy link
Member

https://github.com/electron/electron/blob/master/docs/api/breaking-changes.md

thameera added a commit to thameera/toolbox-desktop that referenced this issue May 1, 2019
The fix in main.js was done because of this:
electron/electron#17241
@bokele
Copy link

bokele commented Jun 2, 2019

in your index.js

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

@Sanjit1
Copy link

Sanjit1 commented Aug 17, 2019

You could use Browserify to fix your issue, had it been a web page or a website. But since you are using Electron to render html, you just need to set node integration to true under web preference: like shown here.

@tolbxela
Copy link

in your index.js

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

Thanks! Have solved my problem.

@smprime
Copy link

smprime commented Oct 23, 2019

npm i @angular-devkit/build-angular -D

@873261648
Copy link

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

@adnan00733
Copy link

Thanks @bokele You are a life saver. :)

@btargac
Copy link

btargac commented Jan 6, 2020

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.

imlucas added a commit to mongodb-js/compass-connect that referenced this issue Jan 8, 2020
See electron/electron#17241

`nodeIntegration` now defaults to `false` so you'd just get an error message like

> Uncaught ReferenceError: require is not defined
@mahdisalmanzade
Copy link

actually i added nodeIntergration = true but dosent work still

vlewin added a commit to vlewin/electron-tray-player that referenced this issue Sep 1, 2020
@wkpdd
Copy link

wkpdd commented Nov 3, 2020

@tolbxela Thanks a lot mate

@the94air
Copy link

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.

@brntsllvn
Copy link

Assuming electron 12.0.0

set contextIsolation: false

Something like...

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

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

@ruymon
Copy link

ruymon commented Mar 9, 2021

@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,

@eric-burel
Copy link

Hi guys, any idea how to debug which package/piece of code is triggering this error? Proposed solutions are hacks, they lead to an insecure behaviour, the app needs an actual update to have a more secure behaviour. But the problem is that it's quite difficult to identify the faulty code.

@btargac
Copy link

btargac commented Aug 24, 2021

where do you get this error ? I guess it is on runtime ? if you can open devtools on the running app then console error should contain the required information about the stack trace with the actual package causing it

@eric-burel
Copy link

eric-burel commented Aug 24, 2021

I'll try to share a screen but it seems to be obsfuscated by webpack dev server, despite having source maps. I'll check in production if the error is clearer. Yes runtime indeed.
For instance this issue may appear with electron-log (fix is documented hopefully)

Edit: it basically look like in this Stack Overflow question: https://stackoverflow.com/questions/59537125/require-doesnt-appear-in-my-code-but-webpack-keeps-throwing-the-error-require
The log are pretty obscure, sadly leading people to keep nodeIntegration and remove contextIsolation in their app.

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

No branches or pull requests