5

I am using Vue CLI 3 and vue-cli-plugin-electron-builder to package my Vue Electron app and I am not able to get my preload.js script for electron working.

main window

win = new BrowserWindow({
  width: 800,
  height: 600
  webPreferences: {
    nodeIntegration: false,
    preload: path.join(__dirname, "/../src/preload.js") // works but window.electron.dialog in undefined
  }
});

preload.js

const { dialog } = require("electron");

window.electron = {};
window.electron.dialog = dialog;

The window.electron.dialog is always undefined in my Vue component - the import is clearly not working. Note that window.electron is defined properly. I must be missing something.

2 Answers 2

4

Add the following lines into the file vue.config.js , if the file does not exist create one in the root folder of your project

module.exports = {
 //...
 pluginOptions: {
  electronBuilder: {
    preload: 'src/preload.js',
    // Or, for multiple preload files:
    // preload: { preload: 'src/preload.js', otherPreload: 'src/preload2.js' }
  }
 }
 //...
}

Check the documentation for more #preload-files

0

The solution was more simple than expected. Imports work in window.onload event.

window.onload = () => {
  const { dialog } = require("electron").remote;

  window.electron = {};
  window.electron.dialog = dialog; // now set properly
};
1
  • 1
    I still can't get this to work... require("electron") returns undefined here. Sep 20, 2020 at 19:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.