Projects in a monorepo often depend on one another. A build for an application might require build artifacts from several libraries, while unrelated builds can run at the same time. Nx uses the project graph and task pipeline configuration to determine that order.
In this project graph, myreactapp depends on feat-products, which depends on shared-ui:
A script could build shared-ui, then feat-products, and then myreactapp. That script would duplicate information already available in the project graph and would need updates whenever project dependencies changed.
Task pipelines express the ordering rule instead. Nx applies the rule to the current project graph and runs as many independent tasks in parallel as the pipeline permits.
Define task dependencies
Section titled “Define task dependencies”Use dependsOn in targetDefaults to define workspace-wide task dependencies:
{ "targetDefaults": { "build": { "dependsOn": ["^build", "prebuild"], }, "test": { "dependsOn": ["build"], }, },}With this configuration, nx test myproj produces the following sequence:
- Nx adds
myproj:testto the task graph. - Because
testdepends onbuild, Nx addsmyproj:build. - Because
builddepends onprebuild, Nx addsmyproj:prebuild. - The
^buildentry addsbuildtasks for projects thatmyprojdepends on. - Nx runs each task when its dependencies are complete.
Nx doesn't wait for every build to finish before starting every test. It runs independent tasks in parallel while respecting the graph constraints.
The caret in ^build means "run the target on project dependencies." Without the caret, prebuild and build refer to targets on the same project.
Define shared rules in nx.json. Use the nx.targets section of package.json, or project.json, when one project needs a different pipeline.
For all dependsOn forms and project-specific examples, see define a task pipeline.