electron-next icon indicating copy to clipboard operation
electron-next copied to clipboard

Unable to use electron from renderer

Open Djiit opened this issue 5 years ago • 3 comments

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).

Djiit avatar Apr 08 '20 12:04 Djiit

did you find any solution?

anktsrkr avatar Apr 26 '20 08:04 anktsrkr

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

Djiit avatar Apr 26 '20 08:04 Djiit

@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!

anktsrkr avatar Apr 26 '20 09:04 anktsrkr