Requiring electron dialog from render process is undefined
On the Renderer process, you must use the Remote module.
const dialog = require('electron').remote.dialog
More info:
Electron Dialog API
Electron Remote API
Electron in the newest version have changed the way of requiring the modules. The modules are encapsulated within the electron Name space.
// for getting the electrons modules here the new method now i'm using 1.7.12 Electron version (i don't know what that will be in the future)
// you require electron first! it's a name space (module)
var electron = require("electron");
var remote = electron.remote; // you get all the subModuls directly as property (cool incapsulation)
//remote = require("remote") ===> will not work!!!!
// for dialog it's the same !! but we now use remote as modul
var dialog = remote.dialog;
Also you can use this syntax, to import several modules with less writing and by gathering them all together:
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;
in place of :
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
//modul for bar menu
const Menu = electron.Menu
I'm surprised none of the answers mention this yet, but since the remote
module has been removed and it's preferable not to use the userland remote
for a variety of reasons discussed here, you'll want to use ipc to have the main process show the dialog.
renderer.js:
const { ipcRenderer } = require("electron");
ipcRenderer.invoke("showDialog", "message");
main.js:
const { ipcMain, dialog } = require("electron");
ipcMain.handle("showDialog", (e, message) => {
dialog.showMessageBox(mainWindow, { message });
});