Porosz
Porosz

Reputation: 81

How to open two seperate nw.js apps

I am building a server and a client nwjs app, but I can't open both at the same time. I wonder if there is any way to do this. I run npm run dev on both of my opened VS Code but when I run this command on the second app it just won't open at all (doesn't matter which one is the second app I would like to run). I tried to build the client app and run it and after it run the server app but it's the same, the second app won't start.

This is my package.json file in both app, I don't know if this helps at all. Only the name is different in the apps (nwjs_client and nwjs_server)

{
  "name": "nwjs_server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nw src/",
    "prod": "nwbuild --platforms win32,win64,osx64,linux32,linux64 --buildDir dist/ src/"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nw": "^0.49.1"
  }
}

I'm willing to accept any answers, I don't know if it is even possible to run 2 different nwjs apps.

Upvotes: 1

Views: 426

Answers (1)

Jaredcheeda
Jaredcheeda

Reputation: 2012

I'm confused by what you are trying to do.

  • If you are trying to run an NW.js that, instead of loading directly from a index.html, it loads displays a page from a local webserver:
    1. Create an server.js that spins up a local web server on a specific port (like 4263 or something not super common).
    2. If you need any node_modules for this (like express) make sure it is a dependency and not a devDependency.
    3. Set your "main" in the package.json to "http://localhost:4263 using the same port as the server
    4. set your "node-main" to "server.js", this will run in the node context before your window is displayed when starting the app.
    5. Set your "node-remote" to "http://localhost:4263" using the same port also. This will allow Node commands to run on that URL when loaded in NW.js.
  • If you are wanting to run two commands at the same time you can:
    1. npm install --save-dev concurrently wait-on. This will install two devDeps
    2. Set your npm script to "start": "concurrently \"npm run serve\" \"wait-on http://localhost:4263 && nw .\""
    3. This will run your npm run serve command, which presumably spins up a local webserver for development, if using something like webpack, this could take a minute. Then it waits until localhost:4263 actually returns a response. Then it launches NW.js

concurrently will also let you run any two (or more) commands at the same time.

Upvotes: 0

Related Questions