Nest.js is a framework designed for building scalable server-side applications. In many ways, Nest is familiar to Angular developers:
- It has excellent TypeScript support.
- Its dependency injection system is similar to the one in Angular.
- It emphasises testability.
- Its configuration APIs are similar to Angular as well.
Many conventions and best practices used in Angular applications can be also be used in Nest.
Setting Up Nest
To create a new workspace with Nest, run the following command:
npx create-nx-workspace my-workspace --preset=nest
Yarn users can use the following command instead:
yarn create nx-workspace my-workspace --preset=nest
Make sure to install the
@nx/nest version that matches the version of
nx in your repository. If the version numbers get out of sync, you can encounter some difficult to debug errors. You can fix Nx version mismatches with this recipe.
To add the Nest plugin to an existing workspace, run one the following commands:
You can add a new Nest application with the following command:
Generating Nest applications has an option to configure other projects in the workspace to proxy API requests. This can be done by passing the
--frontendProject with the project name you wish to enable proxy support for.
nx g @nx/nest:app my-nest-app --frontendProject my-angular-app
You can add a new Nest library with the following command:
To make the library
buildable, use the following command:
To make the library
publishable, use the following command:
nx g @nx/nest:lib my-nest-lib --publishable --importPath=@my-workspace/my-nest-lib
Read more about building and publishing libraries here.
The Nest plugin for Nx extends the generators provided by Nest. Any commands that can be used with the Nest CLI can also be used with the
nx command. The
--project flag should be used for all Nest generators.
--projectis used to infer the root of the project where the generators will generate the files.
You can build an application with the following command:
nx build my-nest-app
This applies to
buildable libraries as well
nx build my-nest-lib
Waiting for other builds
waitUntilTargets option with an array of projects (with the following format:
"project:target") will execute those commands before serving the Nest application.
You can serve an application with the following command:
nx serve my-nest-app
serve command runs the
build target, and executes the application.
By default, the serve command will run in
watch mode. This allows code to be changed, and the Nest application to be rebuilt automatically.
Nest applications also have the
inspect flag set, so you can attach your debugger to the running instance.
Debugging is set to use a random port that is available on the system. The port can be changed by setting the port option in the
serve target in the
project.json. Or by running the serve command with
For additional information on how to debug Node applications, see the Node.js debugging getting started guide.
You can lint an application with the following command:
nx lint my-nest-app
You can lint a library with the following command:
nx lint my-nest-lib
You can run unit test for an application with the following command:
nx test my-nest-app
You can run unit test for a library with the following command:
nx test my-nest-lib