spa5k/nextjs_approuter_electron

Cannot Use other Electron Native Code

Closed this issue · 2 comments

I exposed openDirectory on Main and preload.ts but my frontend is complain that ts: Property 'openDirectory' does not exist on type '{ ipcRenderer: { send: (channel: string, ...args: any[]) => void; }; }'.

Preload.ts

import { contextBridge, ipcRenderer } from "electron";

contextBridge.exposeInMainWorld("electron", {
  ipcRenderer: {
    send: (channel: string, data: any) => ipcRenderer.send(channel, data),
    on: (channel: string, listener: (event: any, ...args: any[]) => void) =>
      ipcRenderer.on(channel, listener),
  },
  openDirectory: () => ipcRenderer.invoke("dialog:openDirectory"),
});

Main.ts


  ipcMain.on("ping", () => console.log("pong"));
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

ipcMain.handle("dialog:openDirectory", async () => {
  const result = await dialog.showOpenDialog({
    properties: ["openDirectory"],
  });
  if (result.canceled) {
    return { canceled: true };
  }
  return { canceled: false, filePath: result.filePaths[0] };
});

Thats typescript issue, you can fix it by adding a .d.ts file

Thats typescript issue, you can fix it by adding a .d.ts file

Thanks it worked I figured out there were two electron.d.ts that's why it was not working