Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Electron 5.0 changed defaults for nodeIntegration #9

Closed
Pinnock opened this issue Jul 27, 2019 · 12 comments
Closed

Electron 5.0 changed defaults for nodeIntegration #9

Pinnock opened this issue Jul 27, 2019 · 12 comments

Comments

@Pinnock
Copy link

Pinnock commented Jul 27, 2019

According to this StackOverflow post, as of Electron v5, the default for nodeIntegration changed from true to false, so we need to explicitly enable it by changing the following line in main.js (chapter 2):

mainWindow = new BrowserWindow();

to

mainWindow = new BrowserWindow({ webPreferences: { nodeIntegration: true } });

otherwise the script (in index.html) used to display the home folder will not work.

@paulbjensen
Copy link
Owner

Hi,

Thanks for providing that piece of information about the change in defaults. Tomorrow I'll update the code with a change for that configuration change.

@paulbjensen
Copy link
Owner

Afternoon,

Apologies for the delay. I've added the code change now.

@docdayao
Copy link

docdayao commented Apr 4, 2021

I am getting Uncaught ReferenceError: require is not defined at index.html:16

  • index.html:16 has document.write(require('osenv').home());
  • I have done npm install osenv --save

@paulbjensen
Copy link
Owner

Hi @docdayao,

Can you let me know:

  • What Chapter and version of the code you are running (the NW.js or Electron example)
  • What version of Node.js, and either Electron or NW.js you are running
  • On what Operating System, and what version (e.g. Windows 10).

Thanks.

@docdayao
Copy link

docdayao commented Apr 6, 2021 via email

@paulbjensen
Copy link
Owner

Will take a look shortly.

@paulbjensen
Copy link
Owner

@docdayao what version of Electron are you running? I am going to try replicating the error with Electron 12.0.4

@docdayao
Copy link

docdayao commented Apr 26, 2021 via email

@paulbjensen
Copy link
Owner

@docdayao I have figured out the issue.

Electron has made updates to improve the security around running apps. If you want to require Node.js libraries in the renderer process, then you have to pass these parameters to the BrowserWindow instance on line 17:

mainWindow = new BrowserWindow({
	webPreferences: { nodeIntegration: true, contextIsolation: false },
});

The contextIsolation parameter is the one that was missing. There is more information at this link: https://www.electronjs.org/docs/tutorial/quick-start#nodejs-api

@docdayao
Copy link

docdayao commented Apr 27, 2021 via email

@docdayao
Copy link

docdayao commented Apr 27, 2021 via email

@paulbjensen
Copy link
Owner

@docdayao You're welcome.

On the second edition, there isn't one planned, but if Manning Publications want a 2nd edition then it will happen.

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

No branches or pull requests

3 participants