Search code examples
node.jsreactjsnode-webkit

Node-main script not executed with Nw-react-script


I'm trying to create a Desktop App which is using create-nw-react-app and Sqlite.

Since trying to compile native modules with NW.js is a nightmare, I'm trying to start a server with express before the frontend loads using node-main feature in package.json.

But when calling nw-react-scripts start, I found that the node-main script is not executed. If I open a separate terminal running the express server it works.

How can I tell NW to start the server before starting react application?

my package.json

{
  "name": "ultimo",
  "node-main": "api/server.js",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/nw.js": "^0.13.13",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "better-sqlite3": "^8.0.1",
    "body-parser": "^1.20.1",
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "express-session": "^1.17.3",
    "nw-react-scripts": "4.0.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "nw-react-scripts start",
    "build": "nw-react-scripts build",
    "test": "nw-react-scripts test",
    "eject": "nw-react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "nw-react-app",
      "nw-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"
    ]
  },
  "main": "index.html",
  "nwBuilder": {
    "//": "https://github.com/nwjs-community/nw-builder",
    "platforms": [
      "osx64",
      "win32",
      "win64"
    ],
    "version": "latest",
    "flavor": "normal",
    "buildDir": "./build",
    "cacheDir": "./cache",
    "macIcns": "./src/logo.icns"
  },
  "devDependencies": {
    "concurrently": "^7.6.0",
    "wait-on": "^7.0.1"
  }
}

I have tried to modify to script start into concurrently \"node api/server.js\" \"nw-react-scripts start\" and it works, but it's only for development.


Solution

  • You haven't provided enough information. This works just fine with NW.js 0.71.0:

    package.json:

    {
      "name": "test",
      "main": "index.html",
      "node-main": "node-main.js",
    }
    

    node-main.js:

    console.log('hello world');
    

    index.html:

    Test
    

    Run the app, inspect background page, note that it shows "hello world" in the console. If yours doesn't work, there's a bug in api/server.js.

    Edit: This could be caused by nw-react-scripts. Since create-nw-react-app doesn't seem to be maintained anymore, you may want to look into other options. You can use the following as an initial template: https://github.com/nwutils/nw-react-example