13

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 
Chan15
  • 929
  • 2
  • 9
  • 16
  • 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 Jan 09 '23 at 04:00

7 Answers7

18

Try this:

nx run-many --parallel --target=serve --projects=frontend,backend 
kervantas
  • 205
  • 1
  • 6
  • 2
    That command should work but ports conflict when I have multiple nest.js apps. – Kal Jul 26 '21 at 08:02
10
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

Dheeraj kumar Rao
  • 8,132
  • 3
  • 22
  • 24
  • 1
    Can you please [edit] that in to your answer? Treat comments as if they can be deleted at any time. – General Grievance Apr 06 '22 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 Jan 09 '23 at 04:10
  • see this https://nx.dev/reference/nx-json#tasks-runner-options – Dheeraj kumar Rao Jan 09 '23 at 10:51
7

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)

Ahmed El Damasy
  • 671
  • 6
  • 13
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
      },
    
Hoang Subin
  • 6,610
  • 6
  • 37
  • 56
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 
Chathurika Sandarenu
  • 1,368
  • 13
  • 25
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.

aruke
  • 611
  • 1
  • 10
  • 17
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
David Wolf
  • 1,400
  • 1
  • 9
  • 18