With node-integration false, how can I use dialog in the main process?


#1

Hi,

I’m new to Electron. I have an AngularJS app that I moved into Electron. Worked perfect after I told the BrowserWindow that ‘node-integration’: false.

I had to do this because the Angular app dependencies were loaded using Bower and simple script tags pointing to the js files we needed like toastr or kendo ui.

But now, we need to take advantage of the native dialog feature of Electron.

With ‘node-integration’: false, I can no loner use required(’ module ') in my render process JS so that I can use “remove” to talk to “dialog”

Is there a way to get to “remote” and “dialog” with ‘node-integration’: false?

Thank you,

Karl


#2

Maybe you can use a preload script (which always has access to the node api).


#3

John,

From what I’m reading, preload works with web-view, which I’m not using.

Best,

Karl


#4

According to the documentation it also works with BrowserWindow.


#5

Thank you, I’ll give it a go. Having trouble finding an example.


#6

John,

Turns out this is SO simple, just hard to understand what the capabilities are.

This is all I had to do:

  1. modify the options passed to the BrowserWindow ctor by adding the preload

    mainWindow = new BrowserWindow({
        width: 1400,
        height: 1000,
        center: true,
        resizable: true,
        'node-integration': false,
        **preload: path.resolve(path.join(__dirname, 'preload.js')),**
    });
    

This is the contents of the preload.js file

global.ELECTRON_SHELL = require('shell');

This is how I used it to open the url in an external browser.

function showUrl(url) {
    ELECTRON_SHELL.openExternal(url);
}

Thank you very much for pointing this Electron New Bee in the right path,

Best,

Karl


#7

@john I’m adding one more example.

If you want to access dialog in this scenario you need to change preload.js to:

global.ELECTRON_SHELL = require('shell');
var remote = require('remote');
global.ELECTRON_DIALOG = remote.require('dialog');

Then, just use ELECTRON_DIALOG in you render process js files.

Karl