saltyshiomix / nextron

⚡ Next.js + Electron ⚡
https://npm.im/nextron
MIT License
3.69k stars 215 forks source link

Can't import 'electron' module or use ipcRenderer in nextjs pages folder. I keep getting error: Can't resolve 'fs' #415

Closed Abdul-Asa closed 8 months ago

Abdul-Asa commented 8 months ago

Hi, I'm trying to create a nextron app with ipc communications but I keep getting an error anytime I import anything from the electron module. I've read similar issues on the github repo and even used the ipc example template but that doesn't work and no explicit fix on the issue. I figured this fix will be on the road map but it wasn't stated there either. How can I solve this? Any advice would be helpful. Thanks.

Screenshot 2023-10-11 at 15 41 54 Screenshot 2023-10-11 at 15 43 25
Mrx-dev commented 8 months ago

same problem here

makeusabrew commented 8 months ago

This is the same issue as #413.

Unfortunately, the base template has recently been updated to follow long since established best practices of not allowing the renderer to access "privileged" modules (electron being one of them), but the examples haven't all caught up.

The very short term and not recommended fix is to swap the values of nodeIntegration and contextIsolation in ./main/helpers/create-window.ts. That is, you want them to look like this:

const win = new BrowserWindow({
    ...state,
    ...options,
    webPreferences: {
      ...options.webPreferences,
      nodeIntegration: true,
      contextIsolation: false,
    },
  })

However, you shouldn't do this. As I recommended in #413, have a read of the official Electron IPC documentation, and follow those practices instead. It's not a great deal of work: you just need to expose a limited API to the renderer process via a preload script: https://www.electronjs.org/docs/latest/tutorial/ipc

Hopefully the examples will be updated in time (I would update the IPC one myself if I could find the time!)

saltyshiomix commented 8 months ago

I'll fix the bug soon, sorry for inconvenience 🙇

saltyshiomix commented 8 months ago

@Abdul-Asa CC: @Mrx-dev @makeusabrew

Nextron v8.12.0 is out! Now it supports preload script for secure IPC features :)

If you have time, please try new examples:

npx create-nextron-app test-app --example basic-lang-typescript
Abdul-Asa commented 8 months ago

It's all good and working now Thank you @saltyshiomix and thanks for the advice @makeusabrew

KaldrArt commented 7 months ago

@Abdul-Asa CC: @Mrx-dev @makeusabrew

Nextron v8.12.0 is out! Now it supports preload script for secure IPC features :)

If you have time, please try new examples:

npx create-nextron-app test-app --example basic-lang-typescript

great, just use window.ipc.send() ~ thank you!