electron/forge

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

DiegoLibonati opened this issue · 4 comments

Pre-flight checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project uses.
  • I have searched the issue tracker for a bug that matches the one I want to file, without success.

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"
  }
}

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: #3713

@DiegoLibonati 's solution is helpful for the problem I'm facing; it just needs the following modifications to resolve it.

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

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
  app.quit();
}

Modify to

// src/main.js
import { app, BrowserWindow } from 'electron';
import path from 'node:path';
import started from 'electron-squirrel-startup';

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (started) {
  app.quit();
}