Following the official IPC tutorial worked for me
main process:
import {dialog, ipcMain} from 'electron'
function createWindow () {
mainWindow = new BrowserWindow({/*Your electron window boilerplate*/})
ipcMain.handle('dialog:openDirectory', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
})
if (canceled) {
return
} else {
return filePaths[0]
}
})
}
preload script:
import {contextBridge, ipcRenderer} from 'electron'
contextBridge.exposeInMainWorld('myAPI', {
selectFolder: () => ipcRenderer.invoke('dialog:openDirectory')
})
Now you can call the selectFolder method from your application code and get the user input.
window.myAPI.selectFolder().then(result=>{/* Do something with the folder path*/})