Nx CLI run many command is not working for multiple apps

Viewed 6685

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 
5 Answers

Try this:

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

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)

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

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 

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
      },
    
Related