问题
I'm building an app and all works fine while I'm in developer mode. Everythink works as it should. But when I package my app with electron-builder, app opens but it doesnt start express server and app doesnt work properly.
Here is my package.json code
{
  "name": "artros",
  "version": "1.0.0",
  "description": "Artros",
  "author": "MC3",
  "license": "ISC",
  "main": "start.js",
  "scripts": {
        "pack": "build --dir",
        "dist": "build"
    },
    "build": {
        "appId": "com.artros.app",
        "productName": "Artros",
        "win": {
            "target": "portable",
            "icon": "build/icon.ico"
        },
        "mac": {
            "target": "dmg"
        }
    },
  "dependencies": {
    "body-parser": "^1.18.3",
    "ejs": "^2.5.7",
    "electron-pdf-window": "^1.0.12",
    "express": "^4.16.2",
    "multer": "^1.3.0",
    "nodemailer": "^4.6.4",
    "path": "^0.12.7"
  },
    "devDependencies": {
        "electron": "^1.8.2"
    }
}
and here is my start.js code
 const cluster = require('cluster');
    if (cluster.isMaster) {
      require('./main.js'); // your electron main file
      cluster.fork(); 
    } else {
      require('./app.js'); // your server code
    }
and my main.js code
var electron = require('electron');
var browserWindow = electron.BrowserWindow;
var app = electron.app;
app.on('ready', function(){
    var appWindow;
//appWindow
    appWindow = new browserWindow({
        width:1120,
        height:620,
        webPreferences: {
            plugins: true
        },
        icon: __dirname + '/public/icon/icon.png' 
    });
    appWindow.loadURL('file://' +__dirname + '/public/prva.html');
    //appWindow.webContents.openDevTools();
});
// close app after all windows are closed
    app.on('window-all-closed', () => {
  app.quit()
})
If anybody has any idea what is the problem, please post it. Thanks
回答1:
I had something similar happen to me. The challenge was that if you use fork() the application path changes. So I would recommend that you check __dirname in all of your files especially the ones in your forked process (e. g. app.js). I wouldn't be surprised if some of them don't make sense anymore.
回答2:
I found the solution. The problem really was in my app.js code. At one detination I needed to add (path.join(__dirname, './path/to/file')). Guys thanks for your help.
来源:https://stackoverflow.com/questions/50436594/electron-build-app-doesnt-start-express-server