leo / electron-next

Build Electron apps using Next.js
https://github.com/leo/site/blob/beef3a7dc1dfd435a9d8377e3b1b59761ccb7fc7/pages/2017/electron-next.js
MIT License
548 stars 41 forks source link

Unable to use electron from renderer #28

Open Djiit opened 4 years ago

Djiit commented 4 years ago

Hi,

When importing electron for the renderer :

import electron from "electron";

I get this error :

Module not found: Can't resolve 'fs' 

Did I miss something in the config ?

I checked an old issue that suggested to use target="electron-target" but its outdated. Also, the now-desktop app doesn't use electron in the renderer (e.g. for opening links in default browser).

anktsrkr commented 4 years ago

did you find any solution?

Djiit commented 4 years ago

Nope. @leo , sorry for tagging but did you had a similar problem before ?

anktsrkr commented 4 years ago

@Djiit Since nodeIntegration is disabled, we will not be able to use 'electron' package directly, However you can use any module of electron by exposing it in global.

This is how ipcRenderer is also exposed to renderer process.

Example - Let's say you want to use remote.getCurrentWindow()

Goto preload.js and add these line -

const { ipcRenderer,remote} = require('electron')
process.once('loaded', () => {
  global.ipcRenderer = ipcRenderer,
  global.remote = remote
})

and from renderer process you can use like this - global.remote.getCurrentWindow()

i just figured, hope this helps!