vite-electron-react-starter icon indicating copy to clipboard operation
vite-electron-react-starter copied to clipboard

Uncaught ReferenceError: __dirname is not defined when I try to import electron

Open capaj opened this issue 3 years ago • 5 comments

I get this when I try to add import such as import { ipcRenderer } from 'electron'; :

Uncaught ReferenceError: __dirname is not defined
    at node_modules/electron/index.js (index.js:4)
    at __require (chunk-KYLA5N2V.js?v=0feadafd:12)
    at dep:electron:1

Although electron is in externals for sure

I also tried adding this:

  optimizeDeps: {
    exclude: ['electron'],
  },

to my renderer.vite.config. That resulted in error Uncaught SyntaxError: The requested module '/@fs/home/capaj/work-repos/subz-desktop/node_modules/electron/index.js?v=2d23cc12' does not provide an export named 'default'

Any idea how to resolve this issue?

capaj avatar May 23 '21 12:05 capaj

Electron is in externals by default, Did you upgrade any dependency ?

soulsam480 avatar Jul 21 '21 07:07 soulsam480

I also have this problem when I try to add import { ipcRenderer } from 'electron' . can you solve this problem? thanks @soulsam480

LiXiaoLongsgithub avatar Dec 21 '21 10:12 LiXiaoLongsgithub

Will check.

soulsam480 avatar Dec 21 '21 10:12 soulsam480

Hii @LiXiaoLongsgithub @capaj , the repo has been updated to latest upstream. Can you try the new template now ? If it's possible

soulsam480 avatar Dec 28 '21 03:12 soulsam480

As far as I know you should import backend stuff in the preload package, there you can re-export and import from render to use.

alexweber avatar Jul 15 '22 12:07 alexweber