I am using electron and am trying to open a file browser when a user clicks on button. From the render process I am trying to include the elctron.dialog package like this.
const dialog = require( 'electron' ).dialog; console.log( dialog );
However the result from the console log is undefined
I am absolutely sure I am in the rendering process so I am not sure why this is not working. The documentation suggests that this is the correct way of doing things but it appears to not be working.
This is my package.json
file
{ "name": "my-app", "version": "0.1.0", "main": "./main.js", "scripts": { "start": "electron ." }, "dependencies": { "electron": "^0.4.1" } }
This is my main.js
file
'use strict'; var app = require( 'app' ); var BrowserWindow = require( 'browser-window' ); var ipc = require( 'ipc' ); var mainWindow = null; app.on( 'ready', function () { mainWindow = new BrowserWindow( { frame : true, height: 700, width : 500 } ); mainWindow.loadUrl( 'file://' + __dirname + '/app/index.html' ); mainWindow.openDevTools(); mainWindow.on( 'closed', function () { mainWindow = null; } ); } ); ipc.on( 'close-main-window', function () { app.quit(); } );
this is the rendered process file
// Add your index.js code in this file var ipc = require( 'ipc' ); const dialog = require( 'electron' ).dialog; console.log( dialog );
This is the console
Is this incorrect?
The latest version of electron made some change to dialog call for security, like you can call dialog in the main process (like index.js) but you can't call direct in the renderer process (like render.js), so have to use ipc In this example: the main process handles dialog through a preload and renders access dialog by calling the main process
var {remote, ipcRenderer, someOtherModulFromElectron} = electron; for example in the main.js (main process) we could write such a call: const electron = require ('electron') const {app, BrowserWindow, Menu} = electron;
The trick is to get the renderer talk to main, ask it to invoke the dialog, and send the result (the file the user selected). But we don’t want to pollute main with the configuration every single dialog we plan to open anywhere in the app.
Is this incorrect? 14.6k 22 63 99 On the Renderer process, you must use the Remote module. I posted my solution above. Your method appears to work in some other versions but not in mine. This is now outdated, as remote is no longer part of electron.
On the Renderer process, you must use the Remote module.
const dialog = require('electron').remote.dialog
More info:
Electron Dialog API
Electron Remote API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With