Defining a Task Pipeline
Running a specific task like build
in a monorepo usually involves multiple commands to be run.
If you want to learn more about the concept of a task pipeline and its importance in a monorepo, have a look at our corresponding concept page.
Define task dependencies
1// nx.json
2{
3 ...
4 "targetDefaults": {
5 "build": {
6 "dependsOn": ["^build"]
7 }
8 }
9}
10
Per Project vs Global
Task dependencies can be defined globally for all projects in nx.json
:
1{
2 ...
3 "targetDefaults": {
4 "build": {
5 "dependsOn": ["^build"]
6 }
7 }
8}
9
Or they can be defined per-project in the project.json
or package.json
. If for example you have a prebuild
step for a given project, you can define such relationship as follows:
1{
2 "name": "myproject",
3 "dependencies": {},
4 "devDependencies": {},
5 ...
6 "nx": {
7 "targets": {
8 "build": {
9 "dependsOn": [
10 "prebuild"
11 ]
12 }
13 }
14 }
15}
16
Visualize task dependencies
You can also visualize the actual task graph (alongside the projects) using Nx graph. This can be useful for debugging purposes.
Either run:
❯
npx nx graph
And then select "Tasks" from the top-left dropdown, choose the target (e.g. build
, test
,..) and either show all tasks or select a specific project you're interested in. Here's an example of the playwright
Nx plugin build
target (in the Nx repo).
Alternatively you can use the Nx Console extension in VSCode or IntelliJ, right-click on the project and select:
It'll then visualize within the IDE: