最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron — Open native dialog window from renderer - Stack Overflow

programmeradmin1浏览0评论

I am trying to open a simple file browser window in Electron. This snippet, straight from the docs:

const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))

doesn't work, and causes:

Uncaught TypeError: Cannot read properties of undefined (reading 'showOpenDialog')

I know there are multiple threads about this, but none of the solutions suggested there seem to work. Adding enableRemoteModule: true to the BrowserWindow definition certainly doesn't. It also doesn't seem to matter if I add .remote or .remote.dialog to the require() line.

I am trying to open a simple file browser window in Electron. This snippet, straight from the docs:

const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))

doesn't work, and causes:

Uncaught TypeError: Cannot read properties of undefined (reading 'showOpenDialog')

I know there are multiple threads about this, but none of the solutions suggested there seem to work. Adding enableRemoteModule: true to the BrowserWindow definition certainly doesn't. It also doesn't seem to matter if I add .remote or .remote.dialog to the require() line.

Share Improve this question edited Nov 21, 2021 at 21:08 custommander 19.1k6 gold badges69 silver badges93 bronze badges asked Nov 21, 2021 at 19:25 Tamás PolgárTamás Polgár 2,2725 gold badges28 silver badges53 bronze badges 4
  • Are you sure you've installed Electron into the project in which this code exists? – CertainPerformance Commented Nov 21, 2021 at 19:26
  • Now I run "npm i electron" just to be sure, but it didn't help. – Tamás Polgár Commented Nov 21, 2021 at 19:34
  • dialog is an API available to the main process only hence why the error. – custommander Commented Nov 21, 2021 at 20:33
  • Yes, I figured this out. I'm now trying to access it in the preloader, to pass it on with contextBridge, and it's still undefined. – Tamás Polgár Commented Nov 21, 2021 at 20:58
Add a ment  | 

1 Answer 1

Reset to default 7

The dialog API is available to the main process only.

You need to have your renderer process asking the main process to open a dialog on its behalf via an IPC channel. This can be done by having your preload script expose an API.

main.js

const {app, BrowserWindow, dialog, ipcMain} = require('electron');
const path = require('path');

app.whenReady().then(() => {
  const win = new BrowserWindow({
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  });
  win.loadFile('renderer.html');
  ipcMain.on('hey-open-my-dialog-now', () => {
    dialog.showOpenDialog({properties: ['openFile', 'multiSelections']})
  });
});

preload.js

const {ipcRenderer, contextBridge} = require('electron');

contextBridge.exposeInMainWorld('MY_APP_NAMESPACE', {
  openDialog() {
    ipcRenderer.send('hey-open-my-dialog-now');
  }
});

renderer.html

<body>
  <button>Ask main process to open a dialog</button>
  <script>
    document.querySelector('button').addEventListener('click', () => {
      MY_APP_NAMESPACE.openDialog();
    });
  </script>
</body>

Then run with:

$ npx electron main.js
发布评论

评论列表(0)

  1. 暂无评论