'Cannot read properties of undefined (reading 'showOpenDialog')

I keep getting this error:

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

How can I call showOpenDialog() with the most minimal changes?

I am using the new Quasar/Electron with Webpack 5.

Here is the openFile() method in my vue file:

    openFile () {
      window.electronApi.dialog.showOpenDialog({
        title: 'Load File',
        properties: ['openFile']
      }).then(result => {
        const filename = result.filePaths[0]

        if (filename) {
          const extension = getExtensionName(filename)
          console.log(extension)
        } else {
          console.log('No file selected')
        }
      }).catch(err => {
        console.log(FILE + 'Error: ' + err)
      })
    },

And here is what is declared in electron-preload.js:


import { contextBridge} from 'electron'

contextBridge.exposeInMainWorld('electronApi', {
  dialog: require('electron').dialog,
  showOpenDialog: require('electron').showOpenDialog
})


Solution 1:[1]

Thank you for @creative learner's idea! This works!

import { contextBridge} from 'electron'
import { dialog } from '@electron/remote' 

contextBridge.exposeInMainWorld('electronApi', {
  dialog: dialog
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kzaiwo