i configure electron with electron builder , like create main.cjs and config package.json , and then run npm run electron-build , created a stepup.exe and then installed like install (headman-system Setup 1.0.0.exe ) , after instaled worked , but when computer shutdown or send that ( headman-system Setup 1.0.0.exe ) to install in another computer , not worked to show content of project means not start localhost , when back to project run npm run electron or npm run start , after run see that installed (headman-system Setup 1.0.0.exe )and shortcut of app is lunched good can see content of project , i don’t now why not automatically start localhost when access to shortcut of app , why always remain on loding.html
i try to fix but i cant always not automatically start localhost :
code :
main.cjs
const { app, BrowserWindow } = require("electron");
const path = require("path");
const { exec } = require("child_process");
const AutoLaunch = require("auto-launch");
const PORT = 50505; // Unique port for localhost
let phpServer;
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(app.getAppPath(), "public/assets/img/1.png"),
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
win.setMenuBarVisibility(false);
win.loadFile("loading.html");
const checkServer = () => {
fetch(`http://localhost:${PORT}`)
.then(() => {
win.loadURL(`http://localhost:${PORT}`); // Load the main content once the server is ready
})
.catch(() => {
setTimeout(checkServer, 1000); // Retry after 1 second if the server is not ready
});
};
checkServer(); // Start checking the server status
}
function startServices() {
const phpPath = path.join(app.getAppPath(), "php", "php.exe");
const publicPath = path.join(app.getAppPath(), "public");
console.log(`PHP Path: ${phpPath}`);
console.log(`Public Path: ${publicPath}`);
phpServer = exec(
`${phpPath} -S localhost:${PORT} -t ${publicPath}`,
(error, stdout, stderr) => {
if (error) {
console.error(`Exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
}
);
phpServer.stdout.on("data", (data) => {
console.log(`PHP Server: ${data}`);
});
phpServer.stderr.on("data", (data) => {
console.error(`PHP Server Error: ${data}`);
});
phpServer.on("close", (code) => {
console.log(`PHP Server exited with code ${code}`);
});
}
app.whenReady().then(() => {
startServices();
createWindow();
// Setup auto-launch
const autoLauncher = new AutoLaunch({
name: "Headman",
path: app.getPath("exe"),
});
autoLauncher.isEnabled().then((isEnabled) => {
if (!isEnabled) autoLauncher.enable();
}).catch((err) => {
console.error(err);
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
if (phpServer) phpServer.kill();
app.quit();
}
});
packge,json
{
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"electron": "electron .",
"electron-build": "electron-builder"
},
"devDependencies": {
"@popperjs/core": "^2.11.6",
"axios": "^1.6.4",
"bootstrap": "^5.2.3",
"electron": "^31.2.1",
"electron-builder": "^24.13.3",
"laravel-vite-plugin": "^1.0.0",
"sass": "^1.56.1",
"vite": "^5.0.0"
},
"name": "headman-system",
"version": "1.0.0",
"description": "<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel Logo"></a></p>",
"main": "electron/main.cjs",
"build": {
"appId": "com.headman.id",
"directories": {
"output": "dist"
},
"files": [
"app/**/*",
"bootstrap/**/*",
"config/**/*",
"database/**/*",
"electron/**/*",
"node_modules/**/*",
"php/**/*",
"public/**/*",
"resources/**/*",
"routes/**/*",
"storage/**/*",
"vendor/**/*",
".editorconfig",
".env",
"artisan",
"composer.json",
"composer.lock",
"loading.html",
"package-lock.json",
"package.json",
"phpunit.xml",
"vite.config.js"
],
"win": {
"icon": "public\assets\img\icon.ico"
},
"nsis": {
"oneClick": false,
"perMachine": false,
"allowToChangeInstallationDirectory": true,
"createDesktopShortcut": true,
"createStartMenuShortcut": true,
"shortcutName": "Headman"
},
"extraResources": [
{
"from": "electron",
"to": "app",
"filter": [
"**/*"
]
}
]
},
"dependencies": {
"anymatch": "^3.1.3",
"asynckit": "^0.4.0",
"auto-launch": "^5.0.6",
"binary-extensions": "^2.3.0",
"braces": "^3.0.3",
"chokidar": "^3.6.0",
"combined-stream": "^1.0.8",
"delayed-stream": "^1.0.0",
"esbuild": "^0.21.5",
"fill-range": "^7.1.1",
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"glob-parent": "^5.1.2",
"immutable": "^4.3.6",
"is-binary-path": "^2.1.0",
"is-extglob": "^2.1.1",
"is-glob": "^4.0.3",
"is-number": "^7.0.0",
"mime-db": "^1.52.0",
"mime-types": "^2.1.35",
"nanoid": "^3.3.7",
"normalize-path": "^3.0.0",
"picocolors": "^1.0.1",
"picomatch": "^2.3.1",
"postcss": "^8.4.39",
"proxy-from-env": "^1.1.0",
"readdirp": "^3.6.0",
"rollup": "^4.18.1",
"source-map-js": "^1.2.0",
"to-regex-range": "^5.0.1",
"vite-plugin-full-reload": "^1.2.0"
},
"keywords": [],
"author": "suli core team",
"license": "ISC"
}
Mister zana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.