forge icon indicating copy to clipboard operation
forge copied to clipboard

Works in dev but errors in prod

Open damms005 opened this issue 10 months ago • 8 comments

Pre-flight checklist

  • [x] I have read the contribution documentation for this project.
  • [x] I agree to follow the code of conduct that this project uses.
  • [x] 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

  • clone the reproduction repo https://github.com/damms005/electron-forge-help
  • install deps and npm start and confirm it works
  • build with npm run make and run the executable in ./out/make/...
  • you should get error similar to screenshot provided

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

damms005 avatar Apr 01 '25 06:04 damms005

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

Sandeep-android-2000 avatar Apr 03 '25 06:04 Sandeep-android-2000

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 avatar Apr 05 '25 03:04 deadcoder0904

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)

deadcoder0904 avatar Apr 05 '25 03:04 deadcoder0904

@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 avatar Apr 05 '25 05:04 Sandeep-android-2000

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

Sandeep-android-2000 avatar Apr 05 '25 05:04 Sandeep-android-2000

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 avatar Apr 05 '25 15:04 deadcoder0904

@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

deadcoder0904 avatar Apr 05 '25 15:04 deadcoder0904

https://github.com/electron/forge/issues/3738#issuecomment-2788643960

araera111 avatar Apr 15 '25 05:04 araera111