[Vuejs]-IPC in electron renderer throws error from missing function __dirname

0👍

You have 2 different issues here:

  • the correct webpack configuration to support node.js code
  • missing node integration to use node API like require

The stacktrace you are seeing here likely comes from an incorrect webpack configuration. Unless told otherwise, webpack tries to replace __dirname with something different. Here we don’t want that – node provides __dirname and we want to use it, so we have to tell webpack to leave __dirname alone.

You’ll find an example in the webpack documentation.

For webpack 5 adding a node section should help:

module.exports = {
  //...
  node: {
    global: false,
    __filename: false,
    __dirname: false,
  }
};

After you solved this problem you’ll likely fall over the issue that your browser window does not know require. You can reintroduce specific node API like the IPC by using a preload script. Don’t activate the full node integration without knowing what you are doing.

For an example, have a look at this answer.

Leave a comment