[Vuejs]-Import { ipcRenderer } from 'electron' produces this error: __dirname is not defined

0๐Ÿ‘

__dirname is a NodeJS variable, in recent electron versions, node integration is disabled by default. When opening your BrowserWindow, you should add the following to the options:

webpreferences:{
    nodeIntegration: true
}

This is however STRONGLY DISCOURAGED as this opens up security issues.

this seems to solve it for most people (for me sadly enough i now get the next error:
fs.existsSync is not a function)

a better solution i to change your bundler to the correct build mode. You should not be building for node but for web, so target:esnext or something.

if something requires node access, this should be solved by running it in the background thread or the preload scripts.

0๐Ÿ‘

You can apply the solution described on this post
How to import ipcRenderer in vue.js ? __dirname is not defined

In this way you can call this method from vue files:

window.ipcRenderer.send(channel, args...)

Just make sure you configure preload.js on vue.config.js:

// vue.config.js - project root
module.exports = {
  pluginOptions: {
    electronBuilder: {
       preload: 'src/preload.js'  //make sure you have this line added
    }
  }
}

Leave a comment