0

Since Electron's recent version 5 have nodeIntegration default to false due to security reasons, which is the recommended way to access node modules? Is there a way to communicate with the main process without nodeIntegration?

Paulo Henrique
  • 938
  • 1
  • 13
  • 19

1 Answers1

1

Using preload script you can communicate with the main process, by importing only the ipcRenderer object to the window.

To do that you have to specify the preload script absolute path in the browserWindow webPreferences.


  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload : path.join(__dirname , '/preload_script.js')
    }
  })

and in preload_script.js inject the ipcRenderer object

window.ipcRenderer = require('electron').ipcRenderer;

you can use the window.ipcRenderer object in the html script for communication with main process/ or with another renderer process.

Sharvin K
  • 697
  • 3
  • 10
  • 2
    Please provide an example or at least a link to Electron's documentation on how to do that. That would make this answer yet more useful to others... – Alexander Leithner May 31 '19 at 18:41
  • I found a little example of pre load script here: https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content which is a nice link about the subject. But having more examples about it and about ipc would be nice. – Paulo Henrique Jun 01 '19 at 01:08
  • I have added more details in the answer pls see. – Sharvin K Jun 03 '19 at 05:33
  • See also my answer, which provides more info and examples, here: https://stackoverflow.com/questions/52236641/electron-ipc-and-nodeintegration/57656281#57656281 – Luke H Aug 27 '19 at 04:43