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 @nx/nest
Section titled “Setting Up @nx/nest”Generating a new workspace
Section titled “Generating a new workspace”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
Installation
Section titled “Installation”In any Nx workspace, you can install @nx/nest
by running the following command:
nx add @nx/nest
This will install the correct version of @nx/nest
.
Create Applications
Section titled “Create Applications”You can add a new Nest application with the following command:
nx g @nx/nest:app apps/my-nest-app
Application Proxies
Section titled “Application Proxies”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 apps/my-nest-app --frontendProject my-angular-app
Create Libraries
Section titled “Create Libraries”You can add a new Nest library with the following command:
nx g @nx/nest:lib libs/my-nest-lib
To make the library buildable
, use the following command:
nx g @nx/nest:lib libs/my-nest-lib --buildable
To make the library publishable
, use the following command:
nx g @nx/nest:lib libs/my-nest-lib --publishable --importPath=@my-workspace/my-nest-lib
Read more about building and publishing libraries here.
Nest Generators
Section titled “Nest Generators”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.
--project
is used to infer the root of the project where the generators will generate the files.
Using Nest
Section titled “Using Nest”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
Section titled “Waiting for other builds”Setting the 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
The 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.
Debugging
Section titled “Debugging”Nest applications also have the inspect
flag set, so you can attach your debugger to the running instance.
VSCode Integration
Section titled “VSCode Integration”When generating Nest applications, Nx automatically creates a VSCode debugging configuration for seamless development experience:
- Automatic setup: A
.vscode/launch.json
file is created with pre-configured debugging settings. - Smart port allocation: Debug ports are automatically assigned starting from 9229, preventing conflicts between multiple applications.
- Source map support: Webpack configurations include
devtoolModuleFilenameTemplate
for accurate source mapping. - Multi-format support: Supports debugging
.js
,.mjs
, and.cjs
output files.
To debug your Nest application in VSCode:
- Open your workspace in VSCode.
- Set breakpoints in your TypeScript source files.
- Go to the Debug panel
(Ctrl+Shift+D)
. - Select "Debug
{your-app-name}
with Nx" from the dropdown. - Click the play button or press
F5
.
The debugger will automatically:
- Start your application with
nx serve
. - Attach to the Node.js debugger on the allocated port.
- Map compiled JavaScript back to your TypeScript source files.
Manual Debugging
Section titled “Manual Debugging”For advanced debugging scenarios, you can manually configure the debug port by setting the port option in the serve
target in the project.json
, or by running the serve command with --port <number>
.
For additional information on Node.js debugging, 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
Unit Test
Section titled “Unit Test”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
Using CLI Plugins
Section titled “Using CLI Plugins”Nest supports the use of various CLI plugins to enhance the development experience. Plugins can be configured via transformers property in NxWebpackPlugin. As an example, to set up a Swagger plugin, modify the Nest application's Webpack configuration as follows:
const { NxWebpackPlugin } = require('@nx/webpack');
module.exports = { // ... plugins: [ new NxWebpackPlugin({ // ... transformers: [ { name: '@nestjs/swagger/plugin', options: { dtoFileNameSuffix: ['.dto.ts', '.entity.ts'], }, }, ], }), ],};
Deployment
Section titled “Deployment”Ensuring a smooth and reliable deployment of a Nest.js application in a production environment requires careful planning and the right strategy. Depending on your specific needs and infrastructure, you can choose from several deployment approaches. Below are four commonly used methods:
Using Docker: Create a Dockerfile that specifies the application's environment and dependencies. Build a Docker image and optionally push it to a container registry. Deploy and run the Docker container on the server. Utilize the
@nx/node:setup-docker
generator to streamline the Docker setup process.Installing Dependencies on the Server: Transfer the build artifacts to the server, install all dependencies using the package manager of your choice, and start the application. Ensure that NxAppWebpackPlugin is configured with
generatePackageJson: true
so that the build artifacts directory includespackage.json
andpackage-lock.json
(or the equivalent files for other package managers).Transferring Pre-installed Dependencies: Install dependencies during the build process, and transfer the build artifacts along with the
node_modules
directory to the server. Typically, the artifacts are archived for faster transfer and then unarchived on the server.Bundling Dependencies: By default, Nx/Nest creates a setup that externalizes all dependencies, meaning they are not included in the bundle. This behavior can be adjusted using the
externalDependencies
parameter in the webpack configuration with NxAppWebpackPlugin. After bundling, transfer the package to the server and start the application.