Commands and tasks in NX
nx
nx

In NX a command represents something like nx run header:build, whereas build is the target, header is the project and header:build is the task.

Running a single task on a single project

nx mytarget myproject

Running multiple tasks on a single project

Running a single task on all projects

nx run-many --target=mytarget

Running a single task on all projects affected in a PR:

nx affected --target=mytarget

There are some predefined tasks:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
yarn nx run myapp:serve
yarn nx run myapp:lint
yarn nx run myapp:test
yarn nx run myapp:serve yarn nx run myapp:lint yarn nx run myapp:test
yarn nx run myapp:serve
yarn nx run myapp:lint
yarn nx run myapp:test

Tasks can be defined as npm scripts in a project’s package.json file or as targets in a project.json file, so have a look at /apps/myapp/project.json to see all available commands or modify them there. If you installed storybook then that would be the place to change the storybook.options.port.

A target in a project.json might look like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"targets": {
"build": {
"executor": "nx:run-commands",
"options": {
"command": "webpack -c webpack.conf.js"
}
},
"test": {
"executor": "@nrwl/jest:jest",
"options": {
"codeCoverage": true
}
}
}
}
{ "targets": { "build": { "executor": "nx:run-commands", "options": { "command": "webpack -c webpack.conf.js" } }, "test": { "executor": "@nrwl/jest:jest", "options": { "codeCoverage": true } } } }
{
  "targets": {
    "build": {
      "executor": "nx:run-commands",
      "options": {
        "command": "webpack -c webpack.conf.js"
      }
    },
    "test": {
      "executor": "@nrwl/jest:jest",
      "options": {
        "codeCoverage": true
      }
    }
  }
}

Running commands parallelly

We can call the serve command (aka target) for more than just one app/project and have them run parallelly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
yarn nx run-many --target=serve --projects=my-api,my-app --parallel=true
yarn nx run-many --target=serve --projects=my-api,my-app --parallel=true
yarn nx run-many --target=serve --projects=my-api,my-app --parallel=true

Creating custom commands

You can create a custom commands, for example one that cares of serving the app and api together. Edit a project.json of one of your libs or apps:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"targets": {
//...
"serveCraAndStorybook" : {
"builder" : "@nrwl/workspace:run-commands",
"options": {
"commands" : [
{
"command": "nx run ui:storybook"
},
{
"command": "nx run pr-cra:serve"
}
]
}
},
//...
}
"targets": { //... "serveCraAndStorybook" : { "builder" : "@nrwl/workspace:run-commands", "options": { "commands" : [ { "command": "nx run ui:storybook" }, { "command": "nx run pr-cra:serve" } ] } }, //... }
"targets": {
//...
    "serveCraAndStorybook" : {
      "builder" : "@nrwl/workspace:run-commands",
      "options": {
        "commands" : [
          {
            "command": "nx run ui:storybook"
          },
          {
            "command": "nx run pr-cra:serve"
          }
        ]
      }
    },
//...
}

About Author

Mathias Bothe To my job profile

I am Mathias from Heidelberg, Germany. I am a passionate IT freelancer with 15+ years experience in programming, especially in developing web based applications for companies that range from small startups to the big players out there. I create Bosycom and initiated several software projects.