electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.41k stars 507 forks source link

Error: Cannot find module ´electron-squirrel-startup´ - Vite or Vite with Typescript on execute .exe #3714

Open DiegoLibonati opened 2 days ago

DiegoLibonati commented 2 days ago

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:

image

This ERROR occurs when using vite or vite-typescript templates. If we use webpack or webpack-typescript it works without problems.

Steps to reproduce

  1. Open a terminal and execute: npm init electron-app@latest my-app -- --template=vite or npm init electron-app@latest my-app -- --template=vite-typescript
  2. Execute: npm run make
  3. Execute the .exe created by npm run make
  4. ¡ERROR!

Additional information

Files: image

Package json:

{
  "name": "my-app",
  "productName": "my-app",
  "version": "1.0.0",
  "description": "My Electron application description",
  "main": ".vite/build/main.js",
  "scripts": {
    "start": "electron-forge start",
    "package": "electron-forge package",
    "make": "electron-forge make",
    "publish": "electron-forge publish",
    "lint": "echo \"No linting configured\""
  },
  "devDependencies": {
    "@electron-forge/cli": "^7.5.0",
    "@electron-forge/maker-deb": "^7.5.0",
    "@electron-forge/maker-rpm": "^7.5.0",
    "@electron-forge/maker-squirrel": "^7.5.0",
    "@electron-forge/maker-zip": "^7.5.0",
    "@electron-forge/plugin-auto-unpack-natives": "^7.5.0",
    "@electron-forge/plugin-fuses": "^7.5.0",
    "@electron-forge/plugin-vite": "^7.5.0",
    "@electron/fuses": "^1.8.0",
    "electron": "32.1.2",
    "vite": "^5.0.12"
  },
  "keywords": [],
  "license": "MIT",
  "dependencies": {
    "electron-squirrel-startup": "^1.0.1"
  }
}
laa-1 commented 1 day 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.

DiegoLibonati commented 1 day ago

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.

erickzhao commented 1 day ago

I think this one has a fix: https://github.com/electron/forge/pull/3713