content/deployment.md
When you're ready to deploy your NestJS application to production, there are key steps you can take to ensure it runs as efficiently as possible. In this guide, we'll explore essential tips and best practices to help you deploy your NestJS application successfully.
Before deploying your NestJS application, ensure you have:
info Hint If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. With Mau, deploying your NestJS application is as simple as clicking a few buttons and running a single command:
bash$ npm install -g @nestjs/mau $ mau deployOnce the deployment is complete, you'll have your NestJS application up and running on AWS in seconds!
To build your NestJS application, you need to compile your TypeScript code into JavaScript. This process generates a dist directory containing the compiled files. You can build your application by running the following command:
$ npm run build
This command typically runs the nest build command under the hood, which is basically a wrapper around the TypeScript compiler with some additional features (assets copying, etc.). In case you have a custom build script, you can run it directly. Also, for NestJS CLI mono-repos, make sure to pass the name of the project to build as an argument (npm run build my-app).
Upon successful compilation, you should see a dist directory in your project root containing the compiled files, with the entry point being main.js. If you have any .ts files located in the root directory of your project (and your tsconfig.json configured to compile them), they will be copied to the dist directory as well, modifying the directory structure a bit (instead of dist/main.js, you will have dist/src/main.js so keep that in mind when configuring your server).
Your production environment is where your application will be accessible to external users. This could be a cloud-based platform like AWS (with EC2, ECS, etc.), Azure, or Google Cloud, or even a dedicated server you manage, such as Hetzner.
To simplify the deployment process and avoid manual setup, you can use a service like Mau, our official platform for deploying NestJS applications on AWS. For more details, check out this section.
Some of the pros of using a cloud-based platform or service like Mau include:
On the other hand, cloud-based platforms are typically more expensive than self-hosting, and you may have less control over the underlying infrastructure. Simple VPS can be a good choice if you're looking for a more cost-effective solution and have the technical expertise to manage the server yourself, but keep in mind that you'll need to handle tasks like server maintenance, security, and backups manually.
While there's technically no difference between development and production in Node.js and NestJS, it's a good practice to set the NODE_ENV environment variable to production when running your application in a production environment, as some libraries in the ecosystem may behave differently based on this variable (e.g., enabling or disabling debugging output, etc.).
You can set the NODE_ENV environment variable when starting your application like so:
$ NODE_ENV=production node dist/main.js
Or just set it in your cloud provider's/Mau dashboard.
To run your NestJS application in production, just use the following command:
$ node dist/main.js # Adjust this based on your entry point location
This command starts your application, which will listen on the specified port (usually 3000 by default). Ensure that this matches the port you’ve configured in your application.
Alternatively, you can use the nest start command. This command is a wrapper around node dist/main.js, but it has one key difference: it automatically runs nest build before starting the application, so you don’t need to manually execute npm run build.
Health checks are essential for monitoring the health and status of your NestJS application in production. By setting up a health check endpoint, you can regularly verify that your app is running as expected and respond to issues before they become critical.
In NestJS, you can easily implement health checks using the @nestjs/terminus package, which provides a powerful tool for adding health checks, including database connections, external services, and custom checks.
Check out this guide to learn how to implement health checks in your NestJS application, and ensure your app is always monitored and responsive.
Logging is essential for any production-ready application. It helps track errors, monitor behavior, and troubleshoot issues. In NestJS, you can easily manage logging with the built-in logger or opt for external libraries if you need more advanced features.
Best practices for logging:
info, warn, error) and disable debug or verbose logs in production to reduce noise.info Hint If you're using AWS (with Mau or directly), consider JSON logging to make it easier to parse and analyze your logs.
For distributed applications, using a centralized logging service like ElasticSearch, Loggly, or Datadog can be incredibly useful. These tools offer powerful features like log aggregation, search, and visualization, making it easier to monitor and analyze your application's performance and behavior.
Scaling your NestJS application effectively is crucial for handling increased traffic and ensuring optimal performance. There are two primary strategies for scaling: vertical scaling and horizontal scaling. Understanding these approaches will help you design your application to manage load efficiently.
Vertical scaling, often referred to as "scaling up" involves increasing the resources of a single server to enhance its performance. This could mean adding more CPU, RAM, or storage to your existing machine. Here are some key points to consider:
Example: If your NestJS app is hosted on a virtual machine and you notice that it’s running slowly during peak hours, you can upgrade your VM to a larger instance with more resources. To upgrade your VM, just navigate to your current provider's dashboard and select a larger instance type.
Horizontal scaling, or "scaling out" involves adding more servers or instances to distribute the load. This strategy is widely used in cloud environments and is essential for applications expecting high traffic. Here are the benefits and considerations:
Example: For a NestJS application experiencing high traffic, you can deploy multiple instances of your app in a cloud environment and use a load balancer to route requests, ensuring that no single instance becomes a bottleneck.
This process is straightforward with containerization technologies like Docker and container orchestration platforms such as Kubernetes. Additionally, you can leverage cloud-specific load balancers like AWS Elastic Load Balancing or Azure Load Balancer to distribute traffic across your application instances.
info Hint Mau offers built-in support for horizontal scaling on AWS, allowing you to easily deploy multiple instances of your NestJS application and manage them with just a few clicks.
There are a few more tips to keep in mind when deploying your NestJS application:
Docker is a platform that uses containerization to allow developers to package applications along with their dependencies into a standardized unit called a container. Containers are lightweight, portable, and isolated, making them ideal for deploying applications in various environments, from local development to production.
Benefits of Dockerizing your NestJS application:
To install Docker, follow the instructions on the official website. Once Docker is installed, you can create a Dockerfile in your NestJS project to define the steps for building your container image.
The Dockerfile is a text file that contains the instructions Docker uses to build your container image.
Here's a sample Dockerfile for a NestJS application:
# Use the official Node.js image as the base image
FROM node:20
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install the application dependencies
RUN npm install
# Copy the rest of the application files
COPY . .
# Build the NestJS application
RUN npm run build
# Expose the application port
EXPOSE 3000
# Command to run the application
CMD ["node", "dist/main"]
info Hint Make sure to replace
node:20with the appropriate Node.js version you're using in your project. You can find the available Node.js Docker images on the official Docker Hub repository.
This is a basic Dockerfile that sets up a Node.js environment, installs the application dependencies, builds the NestJS application, and runs it. You can customize this file based on your project requirements (e.g., use different base images, optimize the build process, only install production dependencies, etc.).
Let's also create a .dockerignore file to specify which files and directories Docker should ignore when building the image. Create a .dockerignore file in your project root:
node_modules
dist
*.log
*.md
.git
This file ensures that unnecessary files are not included in the container image, keeping it lightweight. Now that you have your Dockerfile set up, you can build your Docker image. Open your terminal, navigate to your project directory, and run the following command:
docker build -t my-nestjs-app .
In this command:
-t my-nestjs-app: Tags the image with the name my-nestjs-app..: Indicates the current directory as the build context.After building the image, you can run it as a container. Execute the following command:
docker run -p 3000:3000 my-nestjs-app
In this command:
-p 3000:3000: Maps port 3000 on your host machine to port 3000 in the container.my-nestjs-app: Specifies the image to run.Your NestJS application should now be running inside a Docker container.
If you want to deploy your Docker image to a cloud provider or share it with others, you'll need to push it to a Docker registry (like Docker Hub, AWS ECR, or Google Container Registry).
Once you decide on a registry, you can push your image by following these steps:
docker login # Log in to your Docker registry
docker tag my-nestjs-app your-dockerhub-username/my-nestjs-app # Tag your image
docker push your-dockerhub-username/my-nestjs-app # Push your image
Replace your-dockerhub-username with your Docker Hub username or the appropriate registry URL. After pushing your image, you can pull it on any machine and run it as a container.
Cloud providers like AWS, Azure, and Google Cloud offer managed container services that simplify deploying and managing containers at scale. These services provide features like auto-scaling, load balancing, and monitoring, making it easier to run your NestJS application in production.
Mau is our official platform for deploying NestJS applications on AWS. If you're not ready to manage your infrastructure manually (or just want to save time), Mau is the perfect solution for you.
With Mau, provisioning and maintaining your infrastructure is as simple as clicking just a few buttons. Mau is designed to be simple and intuitive, so you can focus on building your applications and not worry about the underlying infrastructure. Under the hood, we use Amazon Web Services to provide you with a powerful and reliable platform, while abstracting away all the complexity of AWS. We take care of all the heavy lifting for you, so you can focus on building your applications and growing your business.
Mau is perfect for startups, small-to-medium businesses, large enterprises, and developers who want to get up and running quickly without having to spend a lot of time on learning and managing infrastructure. It's incredibly easy to use, and you can have your infrastructure up and running in minutes. It also leverages AWS behind the scenes, giving you all the advantages of AWS without the hassle of managing its complexities.
<figure></figure>With Mau, you can:
To deploy your NestJS application with Mau, just run the following command:
$ npm install -g @nestjs/mau
$ mau deploy
Sign up today and Deploy with Mau to get your NestJS applications up and running on AWS in minutes!