Nx CLI run many command is not working for multiple apps
Asked Answered
M

8

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 
Mustachio answered 30/4, 2021 at 9:55 Comment(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.Tableau
J
23

Try this:

nx run-many --parallel --target=serve --projects=frontend,backend 
Jaqitsch answered 19/5, 2021 at 12:24 Comment(1)
That command should work but ports conflict when I have multiple nest.js apps.Despotism
R
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

Rasure answered 5/4, 2022 at 15:46 Comment(3)
Can you please edit that in to your answer? Treat comments as if they can be deleted at any time.Harbert
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)Tableau
see this nx.dev/reference/nx-json#tasks-runner-optionsRasure
V
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)

Videogenic answered 28/12, 2021 at 20:30 Comment(0)
O
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
      },
    
Ocker answered 14/9, 2022 at 16:38 Comment(1)
I tried the maxPrallel and it worked, and don't keep any other parallel related property flags.Enki
S
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 
Silurid answered 16/12, 2021 at 11:12 Comment(0)
B
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.

Birkner answered 18/12, 2022 at 3:22 Comment(0)
R
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
Regrate answered 24/8, 2023 at 16:28 Comment(0)
D
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

Dariusdarjeeling answered 4/9, 2023 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.