Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requiring electron dialog from render process is undefined

Tags:

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?

like image 919
Subtubes Avatar asked Apr 15 '16 02:04

Subtubes


People also ask

How to call dialog in renderer process?

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

How to call electron from another process in JavaScript?

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;

How to get renderer to talk to main in Android?

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 the remote module still part of the electron process?

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.


1 Answers

On the Renderer process, you must use the Remote module.

const dialog = require('electron').remote.dialog  

More info:
Electron Dialog API
Electron Remote API

like image 73
Philip Avatar answered Sep 27 '22 22:09

Philip