蔡奇霖
蔡奇霖

Reputation: 13

electron remote is undefined with enableremotemodule = true

I am trying to use dialog with "electron": "^13.1.4" , but got an error Uncaught TypeError: Cannot read property 'dialog' of undefined. , Even if I set enableremotemodule = true.

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
......
......

mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        contextIsolation: false,
        enableremotemodule: true,
        nodeIntegration: true,
    }
});
    

and this is the code call the electron remote and got undefined

import { OpenDialogOptions, remote } from 'electron';
.......
.......
openFile() {
    let options: OpenDialogOptions = {};
    console.log(remote); // log undefined
    remote.dialog.showOpenDialog(options).then((filePath) => {
        console.log(filePath);
    });
}

Upvotes: 1

Views: 2964

Answers (2)

Gpak
Gpak

Reputation: 3390

If you are using elect 14.0 or above remote is no longer part of core electron but you can include

const {dialog} = require('@electron/remote');

you should also initialize remote in the main process and allow your window to execute remote

In your main process do this

require("@electron/remote/main").initialize();
const mainRemote = require("@electron/remote/main");
mainRemote.enable(win.webContents);

In your renderer process

const remote = window.require("@electron/remote");
const { getCurrentWebContents, getCurrentWindow, dialog } = remote;

const webContents = getCurrentWebContents();
const currentWindow = getCurrentWindow();

//now show your dialog
   const response = dialog.showMessageBoxSync(currentWindow, {
        buttons: ["Yes", "No"],
        message: "Do you really want to save?",
        title: "Confirm order"
      });

Note: enableRemoteModule no longer works in electron version >= 14.0

Reference

Upvotes: 1

Dinesh s
Dinesh s

Reputation: 377

What is OpenDialogOptions there is no item in electron documentation try with

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

and change the case in main.js as enableRemoteModule: true

Please refer this Dialog documentation by electron

Upvotes: 1

Related Questions