17

I have tried using Nx in an attempt to make use of Monorepos. I have been facing an issue to serve multiple apps via nx run-many command. Can anyone correct me if I'm doing something wrong?

Command used: nx run-many --target=serve --all

I can see the Nx Console logging all the available apps but only running one

>  NX  Running target serve for projects:
  - app1
  - app2
———————————————————————————————————————————————
> nx run app1:serve 
1
  • Did you find a solution for this? What I do is simply run "ng serve", this builds all apps in their respective ports properly, but I'm having issues with the watcher, is not detecting changes in anything than the HOST application, so every time I change something, I need to rebuild all, can't find a solution yet.
    – Yogurtu
    Commented Jan 9, 2023 at 4:00

8 Answers 8

23

Try this:

nx run-many --parallel --target=serve --projects=frontend,backend 
1
  • 2
    That command should work but ports conflict when I have multiple nest.js apps.
    – Kal
    Commented Jul 26, 2021 at 8:02
15
nx run-many --target=serve --all --maxParallel=100 

The default value for --maxParallel is three, it means runs tasks in batches of three by default.

Additional, Exclude few apps to not serve then.

nx run-many --target=serve --all --maxParallel=100 --exclude=app-name

Github

3
  • 1
    Can you please edit that in to your answer? Treat comments as if they can be deleted at any time. Commented Apr 6, 2022 at 12:51
  • On my case, I created the applications by specifying the ports, so I might not have the same scenario than the OP, but when I ran this solution it seems to be working fine, and also it solves the problem that otherwise, it will not watch the changes from remote apps. Yet i'm not sure why if you specify maxParallel=1 or 3, it only starts 1 or 3 apps, so it's not really parallel, it's more how many apps should run (or it's a bug)
    – Yogurtu
    Commented Jan 9, 2023 at 4:10
  • see this nx.dev/reference/nx-json#tasks-runner-options Commented Jan 9, 2023 at 10:51
8

This happens due to port overriding, if you have multiple frontend apps for example they will run on the same port. You can manage every project configuration in project.json file, and there you can handle different port for every project.

example:

"serve": {
  "executor": "@nrwl/web:dev-server",
  "options": {
    "buildTarget": "react-todo:build",
    "hmr": true,
    "port": 3001
  },
  "configurations": {
    "production": {
      "buildTarget": "react-todo:build:production",
      "hmr": false
    }
  }
},

this is a react config in (apps/<Your_Project_Name>/project.json)

5

Update solution in 9/2022.

  1. go to package.json adding this script that allow us to run many project with only one command

    "all": "npx nx run-many --target=serve --all --maxParallel=100"

  2. inside apps folder, there are several application, and go to their package.json, and edit `targets -> serve -> options like this sample

      "options": {
        "buildTarget": "your app name:build",
        "hmr": true,
        "port": 4201 // adding this
      },
    
1
  • I tried the maxPrallel and it worked, and don't keep any other parallel related property flags.
    – Dexter
    Commented Feb 16 at 21:34
1

You can change the serving port by editing package.json

"serve": {
      "executor": "@nrwl/web:dev-server",
      "options": {
        "buildTarget": "admin-web:build",
        "port": 4220,
        "hmr": true
      },
      "configurations": {
        "production": {
          "buildTarget": "admin-web:build:production",
          "hmr": false
        }
      }
    }

After that you can run nx run-many

nx run-many --parallel --target=serve --projects=frontend,backend 
0

For now, Remix uses a hardcoded 8002 port for file watcher. When running two or more remix apps at once, either one of the apps (which was started later) would have an error accessing the file server port. To override, add a .env or .env.local file in your respective app directory and add the environment variable REMIX_DEV_SERVER_WS_PORT.

apps/
 - app1
      .env.local -> REMIX_DEV_SERVER_WS_PORT=8003
 - app2
      .env.local -> REMIX_DEV_SERVER_WS_PORT=8004

This worked for me.

0
nx run-many --help

...

      --parallel           Max number of parallel processes [default is 3]                                                [string]
nx run-many --parallel=4 --target=dev --output-style=stream --projects=@example/one,@example/two,@example/three,@example/four
0

According to the latest NX documentation:

nx run-many --targets=serve -p project1 project2 pj3 pj4 --parallel=4

Also instead of nx in terminal you can use node_modules/.bin/nx or npx nx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.