Integrating Electron with a Vue Project

Integrate Vue pages into Electron and understand the differences between development servers, production builds, and resource paths.

🎯 Introduction

The first two articles used simple pages to explain Electron's structure. This article replaces the page with Vue, giving the later IPC, file operations, and desktop interactions a foundation closer to a real project.

By the end of this article, you will be able to:

  • Explain the respective responsibilities of Electron and Vue.
  • Load Vite pages in development and built files in production.
  • Handle differences between development addresses and production paths.
  • Establish a clear place for preload APIs introduced later.

🧱 How Electron and Vue divide responsibilities

Vue handles pages in the renderer process, while Electron manages windows, the application lifecycle, and system capabilities. Think of them as two layers: Vue is the interface inside the window, and Electron is the desktop shell managing that window.

Electron main process
    └── BrowserWindow
          └── Vue page (renderer process)
                └── Limited APIs provided by the preload script

Integrating Electron does not mean Vue pages should directly import Node.js or Electron modules such as fs or dialog. Pages still request system capabilities from the main process through preload scripts and IPC.


🧰 Prepare the Vue project

This example uses Node.js 24.x LTS, Vue 3.5.x, Vite 6.x, and Electron 44.0.0. If you already have a Vue project, confirm that npm run dev and npm run build work.

Create a Vue project:

npm create vite@latest electron-vue-course -- --template vue
cd electron-vue-course
npm install
npm install --save-dev electron@44.0.0

Add the Electron entry point and start command to package.json:

package.json
{
    "main": "electron/main.js",
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "electron": "electron ."
    }
}
Different Vite, Electron, and Node.js versions may use different defaults. Pin versions while learning, and refer to the files actually generated in your project if command-line prompts change.

🛠 Load the Vite page in development

Create electron/main.js in the project root:

electron/main.js
const { app, BrowserWindow } = require('electron/main');

const VITE_DEV_SERVER_URL = 'http://localhost:5173';

function createWindow() {
    const win = new BrowserWindow({
        width: 960,
        height: 640,
    });

    win.loadURL(VITE_DEV_SERVER_URL);
}

app.whenReady().then(() => {
    createWindow();
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

Start Vite first, then Electron:

npm run dev
npm run electron

The Electron window now displays the Vue page. When you edit src/App.vue, Vite updates the page without repackaging the Electron app.


📦 Load built files in production

Use loadURL to load the Vite server during development and load dist/index.html in production. An environment variable can distinguish the two cases:

electron/main.js
const { app, BrowserWindow } = require('electron/main');
const path = require('node:path');

function createWindow() {
    const win = new BrowserWindow({
        width: 960,
        height: 640,
    });

    if (process.env.VITE_DEV_SERVER_URL) {
        win.loadURL(process.env.VITE_DEV_SERVER_URL);
        return;
    }

    win.loadFile(path.join(__dirname, '../dist/index.html'));
}

Focus on the two loading modes rather than memorizing one fixed path: connect to a server during development and read local files after building. When packaging later with Forge or another tool, check that dist is copied correctly for the final directory structure.

Running npm run electron alone does not start Vite or set VITE_DEV_SERVER_URL automatically. Development scripts usually need tooling to coordinate these steps. For now, run them separately so each part is clear.

🧾 Summary

  • Vue handles renderer pages, while Electron provides desktop windows and system capabilities.
  • Development usually loads the Vite server with loadURL.
  • Production usually loads built HTML with loadFile.
  • Using Vue does not give pages direct access to Node.js or Electron modules.
  • The preload script remains the entry point for pages requesting desktop capabilities.

❓ Questions and answers

Q1: Why not use Vue from the beginning?

The introductory articles use minimal HTML to explain how Electron works without requiring you to learn two tools at once. Adding Vue here lets subsequent examples use a real frontend project structure.

Q2: Why do development and production use different loading methods?

Development needs Vite's hot updates. Production has no development server, so it reads built local files instead.

Q3: Can Vue components import electron directly?

This is not recommended. Pages should access system capabilities only through purpose-specific methods exposed by the preload script. The communication pattern is explained in detail later.


🧪 Practice

Exercise 1: Edit src/App.vue and change the page heading to “My first Vue desktop app”.

Exercise 2: Add a title option to BrowserWindow and observe the window title:

const win = new BrowserWindow({
    // Write your code here
});

🎉 Your Vue page now runs inside an Electron window. Next, we will complete window lifecycle handling for development and production.