Open DiegoLibonati opened 2 days ago
I encountered the same problem, I built an electron application based on vite according to the documentation on the official website, and completed the steps required to integrate vue3 according to the documentation on the official website, it can start normally during development, but the package built application, when starting, it will prompt that the error of "electron-squirrel-startup" was not found.
I found a solution for when using VITE. Vite does not handle imports as well as webpack does.
To quickly fix this bug with VITE, in the main.ts or main.js
file instead of using require(“electron-squirrel-startup”)
we can use commonjs import as follows:
// @ts-expect-error -> In vite there are no types for the following line. Electron forge error
import started from "electron-squirrel-startup";
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (started) app.quit();
My main.ts
:
import { app, BrowserWindow } from "electron";
// @ts-expect-error -> In vite there are no types for the following line. Electron forge error
import started from "electron-squirrel-startup";
import path from "path";
import express from "express";
import { APP_CONFIG } from "@/src/config/config";
declare const MAIN_WINDOW_VITE_DEV_SERVER_URL: string;
declare const MAIN_WINDOW_VITE_NAME: string;
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (started) app.quit();
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
title: "JSON Transformer",
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
// and load the index.html of the app.
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
} else {
mainWindow.loadFile(
path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`)
);
}
// Open the DevTools.
if (APP_CONFIG.GENERAL.DEBUG_MODE) mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", createWindow);
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
const server = express();
server.get("/api/data", (req, res) => {
res.json({ message: "Hello from the backend!" });
});
server.listen(3000, () => {
console.log("Backend running on http://localhost:3000");
});
This temporarily fixes the problem until the creators of electron-forge fix it.
I think this one has a fix: https://github.com/electron/forge/pull/3713
Pre-flight checklist
Electron Forge version
^7.5.0
Electron version
32.1.2
Operating system
Windows 11
Last known working Electron Forge version
No response
Expected behavior
Open executable successfully
Actual behavior
This error occurs after using the command:
electron-forge make
. Once the application is built, when double clicking on the.exe
it throws the following error:This
ERROR
occurs when usingvite or vite-typescript
templates. If we use webpack or webpack-typescript it works without problems.Steps to reproduce
npm run make
npm run make
Additional information
Files:
Package json: