Skip to content

Works in dev but errors in prod #3902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
damms005 opened this issue Apr 1, 2025 · 8 comments
Open
3 tasks done

Works in dev but errors in prod #3902

damms005 opened this issue Apr 1, 2025 · 8 comments

Comments

@damms005
Copy link

damms005 commented Apr 1, 2025

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.

Forge version

7.8.0

Electron version

35.1.2

Operating system

macOs 15.3.2 (24D81)

Last known working Forge version

No response

Expected behavior

When I build my app, it should work like it does in dev

Actual behavior

When I build the app using electron-forge make and run the final binary at out/make/electron-forge-help-1.0.0-arm64.dmg, it errors out on startuo with "Cannot find module 'sharp'":

Image

Steps to reproduce

Additional information

It seems #1276 and #1250 have same issues, but they are webpack-based and I am unable to transfer my understanding of them to this vite-based project

@Sandeep-android-2000
Copy link

hey i am also getting this issue and i am working working on this

@deadcoder0904
Copy link
Contributor

i got a similar issue a 2 days ago & i solved it finally asking gemini with help of yek - https://github.com/bodo-run/yek to upload the entire code.

the bug was near isDev variable. it was setting preload.js in production in the wrong place. so it worked in dev but not in prod.

in my case i had to change from ./preload.js to ../preload/preload.js (this might change depending on your vite.config.ts file)

exact same bug. and this is the exact solution.

@deadcoder0904
Copy link
Contributor

i think your (@damms005) error might be at this line - https://github.com/damms005/electron-forge-help/blob/5f5278bf015cdf796b98d24086c8a082c25ea325/src/main.ts#L12 (just use isDev & set production to ../preload/preload.js or something like that)

my exact code was:

const isDev = process.env.NODE_ENV === 'development'

webPreferences: {
			nodeIntegration: false,
			contextIsolation: true,
			preload: isDev
				? path.join(__dirname, '../preload/preload.js')
				: path.join(__dirname, '../preload/preload.js'),
			scrollBounce: true,
		},

Oh looks like I can remove isDev (maybe in your case u might need to add since the first error literally cant find that file)

@Sandeep-android-2000
Copy link

@deadcoder0904 i have tried the fix as suggested by you in my windows os but i am still getting the same error

Image

@Sandeep-android-2000
Copy link

@deadcoder0904 this is my code:
main.ts file

import { app, BrowserWindow, dialog, ipcMain } from 'electron';
import { readFileSync, writeFileSync } from 'node:fs';
import path, { join } from 'node:path';

const isDev = process.env.NODE_ENV === 'development';

const createWindow = () => {
const preloadPath = isDev
? path.join(__dirname, '../.vite/build/preload.js') // Adjust if your build folder differs
: path.join(__dirname, 'preload.js');

const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
sandbox: false,
contextIsolation: true,
preload: preloadPath,
},
});

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)
);
}

mainWindow.webContents.openDevTools();
};

app.on('ready', createWindow);

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

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});

ipcMain.handle('ask-and-write-base64', (event, base64: string) => {
const folder = dialog.showOpenDialogSync(BrowserWindow.getAllWindows()[0], {
properties: ['openDirectory'],
});

const destination = join(folder[0], 'sample.png');
writeFileSync(destination, base64, { encoding: 'base64' });

return destination;
});

ipcMain.handle('get-file-content-as-base64', (event, filePath: string) => {
return readFileSync(filePath);
});

@deadcoder0904
Copy link
Contributor

Do u know where actually preload.js is?

You should do console.log to see what path it provides.

Also, do asar unpack - https://medium.com/@libaration/decompiling-and-repacking-electron-apps-b9bfbc8390d5 to see where the path is (this is the easiest way to debug it)

Its 100% a path issue for the first one.

2nd issue is sharp should be used in only electron main process i think (the node side... filesystem side... backend), not renderer (the react side... frontend) & should use external config somewhere.

Also, try asking AI... it'll easily solve this (it did solve for me... specifically Gemini 2.5 Pro Experimental which is free in ai.dev rn)

@deadcoder0904
Copy link
Contributor

@Sandeep-android-2000 bdw, u can just use https://electron-vite.org/guide/#scaffolding-your-first-electron-vite-project instead to not worry about all this. it supports releases too i think - https://electron-vite.org/guide/distribution

this might be much easier too. i did face lots of errors many years ago with electron-forge fwiw

@araera111
Copy link

#3738 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants