I've packaged an electron-react app with:
electron-packager . app --platform=win32 --arch=x64 --overwrite
Then a app-win32-x64
folder is created containing the executable and some other files:
app-win32-x64 folder
When I run the executable everything works fine.
Then, I copy the app-win32-x64
folder to another path (where my colleges can access the executable). When I double-click on the exe now, the electron app starts, but with a blank screen and after a few seconds it closes again.
Do you have any idea why this happens?
I haven't used any absolute paths in my code.
here is my main.js
:
const { BrowserWindow, app, ipcMain, Notification } = require('electron');
const path = require('path');
var fs = require('fs')
const isDev = !app.isPackaged;
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 800,
backgroundColor: "white",
webPreferences: {
nodeIntegration: false,
worldSafeExecuteJavaScript: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html');
}
if (isDev) {
require('electron-reload')(__dirname, {
electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
})
}
ipcMain.on('notify', (_, message) => {
new Notification({title: 'Notifiation', body: message}).show();
})
ipcMain.on('makeDir', (event, path) => {
fs.mkdir(path, (err) => {
if (err) {
return console.log(err);
}
console.log(`Created directory in ${path} successfully`);
})
})
app.whenReady().then(createWindow)
here is my package.json
:
{
"name": "electron-react-app",
"productName": "App",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --config webpack.common.js --watch",
"start": "electron ."
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"babel-loader": "^9.1.0",
"css-loader": "^6.7.2",
"electron": "^21.3.1",
"electron-packager": "^17.1.1",
"electron-reload": "^2.0.0-alpha.1",
"sass": "^1.56.1",
"sass-loader": "^13.2.0",
"style-loader": "^3.3.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0"
}
}
If you need any other scripts, let me know.
I also tried copying the entire source code folder, not just the app-win32-x64
folder, it still didn't work. Is it maybe because of some security measures in this other directory? Usually executables run there, that's what the directory is for.
Thank you so much in advance! Leon