Photo by Eric Er on Unsplash

On this page

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"
}
]
}
},
//...
}

Search articles

Type a word to search published articles.

Open the search page