electron / forge

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

Forge make combined with vite is creating an incomplete asar #3738

Open steveoh opened 1 month ago

steveoh commented 1 month ago

Pre-flight checklist

Electron Forge version

7.5.0

Electron version

33.0.1

Operating system

windows 11

Last known working Electron Forge version

7.4.0

Expected behavior

npm run make creates an asar file that includes the node_modules and the app can run on windows successfully.

Actual behavior

node_modules aren't present and install fails with ERR_MODULE_NOT_FOUND

Steps to reproduce

  1. npm run make
  2. go to the out folder and find open the nupkg/resources/app.asar file
  3. run npx @electron/asar extract app.asar app
  4. notice there is no node_modules

Additional information

This is only happening on windows as I believe that is the only place that asar is used. We are using vite instead of webpack.

Image

The main.js file from within the asar in the error message imports the following

import "node:path";
import "node:url";
import "electron";
import "electron-window-state";
import "update-electron-app";
import "./main-DWMHPuxT.js";
import "electron-squirrel-startup";
import "fs";
import "path";
import "url";
import "stream";
import "zlib";

Where electron-window-state and update-electron-app and I assume electron-squirrel-startup are not available to be imported.

justgo97 commented 1 month ago

I'm not sure about 7.5.0 but in earlier versions it was needed to move electron-window-state and any other dependency that isn't getting bundled correctly to devDependencies in package.json, This is a known mismatch between vite and webpack in forge.

steveoh commented 1 month ago

This works as expected downgrading to 7.4.0. I'll give this a try but no bundles are getting added to the app.asar file at 7.5.0.

Well now I'm getting a require is not defined error after making the project. :/

XianZhengquan commented 1 month ago

@steveoh I use electron-log and it works fine after downgrading.

knice88 commented 3 weeks ago

I've had this happen to ubuntu as well, but found a way to get the trick: When you run "npm run make" for the first time, open the @electron-forge/plugin-vite configuration, get the .vite/ folder, and delete the out/ folder; Then turn off the @electron-forge/plugin-vite config and run "npm run make" again to get a complete installer

AimForNaN commented 3 weeks ago

Ran into a similar issue with other packages. Found a work-around and modified it to work for me. Seems @electron/packager related.

// forge.config.js

const { spawn } = require('node:child_process');

module.exports = {
    hooks: {
        packageAfterPrune: async (config, build_path) => {
            const vite_config = await import('./vite.preload.config.mjs');
            const external = vite_config?.default?.build?.rollupOptions?.external || [];
            const commands = [
                'install',
                '--no-package-lock',
                '--no-save',
                ...external,
            ];

            return new Promise((resolve, reject) => {
                npm = spawn('npm', commands, {
                    cwd: build_path,
                    stdio: 'inherit',
                    shell: true,
                });

                npm.on('close', (code) => {
                    if (0 === code) {
                        resolve();
                        return;
                    }

                    reject(`Process exited with code: ${code}`);
                });

                npm.on('error', reject);
            });
        },
    },
    // ...
};
hichemfantar commented 2 weeks ago

same issue here, happens when trying to load a json file

const filePath = path.join(__dirname, "schedule.json");
const data = fs.readFileSync(filePath , "utf-8");
hichemfantar commented 2 weeks ago

temp workaround is to disable asar by modifying the following properties in forge.config.ts

const config: ForgeConfig = {
  packagerConfig: {
    asar: false,
  },
  plugins: [
    new FusesPlugin({
      [FuseV1Options.OnlyLoadAppFromAsar]: false,
    }),
  ],
};
an78mK89fy commented 2 days ago

This error is not related to ASAR. Even if ASAR is disabled, the error is still reported. I think it is a vite plug-in problem. It works normally in 7.4.0