Nx CLI

Why use the Nx CLI?

The Nx CLI isn't just another terminal command that accomplishes a predefined task. Developers from Nrwl and the community have created plugins that provide a base level of functionality. In addition, custom generators and executors can be written - expanding the capabilities of the Nx CLI to fit what your organization needs.

The Nx CLI provides commands that fall into three categories:

  • Modifying code using generators
  • Acting on code using executors
  • Understanding the whole codebase

Modifying Code Using Generators

Generators allow developers to automate a code modification task. Generators can be distributed as part of a plugin or developed locally in an Nx workspace. Generators can be composed to create complex workflows and then included in documentation to ensure consistency across the codebase.

Acting on code using executors

Executors are commands that are run that don't affect the actual code. There are built in executors for test, lint, serve and build but custom executors can have any name. Nx automatically caches the output of executors so that re-running the same executor with the same code input will complete in seconds. The paid Nx Cloud offering allows this cache to be shared across every developer in your organization.

Understanding the whole codebase

Nx creates and maintains a dependency graph between projects based on import statements in your code and uses that information to run executors only on the affected projects in a codebase. A visual version of the dependency graph is also available to help developers understand the architecture of the codebase.

Common Commands

Invoke a generator:

nx generate app my-react-app

This command creates a new React app named my-react-app.

Create a workspace generator:

nx generate workspace-generator my-generator
nx workspace-generator my-generator

The first command scaffolds a new customizable workspace generator named my-generator and the second command invokes it.

Update plugins:

nx migrate latest
nx migrate --run-migrations=migrations.json

The first command updates the installed Nx plugin versions and creates a list of generators to keep configuration files up to date. The second command invokes those generators.

Run an executor on one project:

nx run my-app:build
nx build my-app

Both of these commands build the my-app application. Custom executors need to use the more verbose nx run project:target syntax. See the workspace.json documentation for information on configuring executor options.

Run an executor for all affected projects:

nx affected --target=build

This command runs the build executor for all projects that are affected by the current code change.

View the dependency graph:

nx dep-graph
nx affected:dep-graph

The first command launches a web browser with repository's dependency graph rendered visually. The second command renders the same graph with projects affected by the current code change highlighted.

List installed plugins:

nx list

This command lists the currently installed Nx plugins and shows other plugins that are available.