Node server is not running on electron app launch with react
Asked Answered
S

0

1

In my demoApp I want to start node server/server.js automatically when electron app is launched. I have written below code which works fine when I run npm run electron:start on my local system but when I create a build package and run my app exe It does not start the node server/server.js file.

demoApp/public/electron.js

const { app, BrowserWindow, protocol } = require("electron");
const path = require("path");
const child_process = require("child_process");
let serverProcess;

function createWindow() {
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, "preload.js"),
        },
    });
    serverProcess = child_process.spawn("node", [path.join(__dirname, "server", "server.js")]);

    mainWindow.loadURL(!app.isPackaged ? 'http://localhost:3000' : `file://${__dirname}/../build/index.html`);

    // Open Chrome's DevTools in dev env.
    if (!app.isPackaged) {
        mainWindow.webContents.openDevTools();
    }
}

function setupLocalFilesNormalizerProxy() {
    protocol.registerHttpProtocol(
        "file",
        (request, callback) => {
            const url = request.url.substr(8);
            callback({ path: path.normalize(`${__dirname}/${url}`) });
        },
        (error) => {
            if (error) console.error("Failed to register protocol");
        }
    );
}

app.whenReady().then(() => {
    createWindow();
    setupLocalFilesNormalizerProxy();

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

app.on("window-all-closed", function () {
    if (process.platform !== "darwin") {
        app.quit();
    }
    serverProcess.kill();
});

demoApp/package.json

{
  "name": "demoApp",
  "version": "0.1.0",
  "private": true,
  "main": "public/electron.js",
  "homepage": "./",
  "dependencies": {
    "@emotion/react": "^11.10.6",
    "@emotion/styled": "^11.10.6",
    "@mui/icons-material": "^5.11.16",
    "@mui/material": "^5.12.1",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.2.3",
    "child_process": "^1.0.2",
    "cors": "^2.8.5",
    "electron-packager": "^17.1.2",
    "express": "^4.18.2",
    "express-fileupload": "^1.4.3",
    "json-query": "^2.2.2",
    "jsoneditor": "^9.10.0",
    "original-fs": "^1.2.0",
    "react": "^18.2.0",
    "react-bootstrap": "^2.7.4",
    "react-dom": "^18.2.0",
    "react-drag-drop-files": "^2.3.10",
    "react-dropzone": "^14.2.3",
    "react-redux": "^8.0.5",
    "react-router-dom": "^6.10.0",
    "react-scripts": "5.0.1",
    "react-select": "^5.7.3",
    "redux": "^4.2.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "concurrently \"npm run start:react\" \"npm run start:server\"",
    "start:react": "react-scripts start",
    "start:server": "node server/server.js",
    "prebuild": "react-scripts build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "electron:start": "concurrently \"cross-env BROWSER=none npm start\" \"wait-on http://127.0.0.1:3000 && electronmon .\"",
    "build:package:mac": "npm run prebuild && electron-builder -m -c.extraMetadata.main=build/electron.js",
    "build:package:win": "npm run prebuild && electron-builder -w -c.extraMetadata.main=build/electron.js",
    "build:package:linux": "npm run prebuild && electron-builder -l -c.extraMetadata.main=build/electron.js"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "concurrently": "^8.2.1",
    "cross-env": "^7.0.3",
    "electron": "^26.2.0",
    "electron-builder": "^24.6.4",
    "electronmon": "^2.0.2",
    "start-server-and-test": "^2.0.3",
    "wait-on": "^7.0.1"
  },
  "build": {
    "appId": "electron.react.dmd",
    "productName": "DEMO",
    "files": [
      "build/**/*",
      "node_modules/**/*"
    ],
    "directories": {
      "buildResources": "public"
    },
    "mac": {
      "target": "dmg"
    },
    "win": {
      "target": "nsis"
    },
    "linux": {
      "target": "deb"
    }
  }
}
Scurrile answered 7/12, 2023 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.