This article's content
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:

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:

{
  "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:

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:

"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, born 40 years ago in Heidelberg, Germany. Today I am living in Munich and Stockholm. I am a passionate IT freelancer with more than 16 years experience in programming, especially in developing web based applications for companies that range from small startups to the big players out there. I am founder of bosy.com, creator of the security service platform BosyProtect© and initiator of several other software projects.