DEV Community

Cover image for Deploying NestJS Application: Easy and Explained
Jaskirat Grewal
Jaskirat Grewal

Posted on

Deploying NestJS Application: Easy and Explained

Nest: Quick Overview


The biggest rule of programming is--DRY (Don't Repeat Yourself).


  • If one wants to write a more efficient code whilst developing any kind of application, they must practice DRY and SOLID principles.

  • These principles are the base of the famous Model-View-Controller (MVC) design pattern. The Angular Framework for building Frontend is entirely based on this design pattern. It uses concepts of components and services to implement DRY and SOLID Principles.

  • Not only the increase in performance, understanding Angular code is quite easy due to this.


  • There was no backend framework that makes the task easy in the same way as Angular does in frontend. That's where Nest originated.

  • Nest is a backend framework that is heavily inspired by Angular.

    Angular and Nest

  • It uses the concepts of services, controllers, and modules to implement MVC pattern in a way similar to Angular.


    I highly recommend using Nest when working on larger projects rather than using Express.



The focus of this article is on the deployment of Nest Applications, if you want me to post beginner or advanced level tutorials on learning Nest.js, comment below:-)

How to Deploy NestJS Application?: The Theory

When we want to deploy Angular App, what we basically do is we compile the application to JavaScript and then host it on any static application hosting provider like GitHub, AWS S3, etc.

We host Nest Application in a similar way. We compile the Nest Application to JavaScript and host it on Dynamic Application hosting providers like Heroku, A2Hosting, AWS EC2, AWS Elastic Beanstalk, etc.


How to Deploy NestJS Application?: The Platform Generic Steps

  • First, open .gitignore file and remove the following line to enable pushing the compiled javascript files to the remote repository so that we can download them on the server for hosting.
Remove `/dist` from `.gitignore`
  • In package.json make sure that it contains scripts defined as ```json

"build": "nest build",
"start": "nest start",
"start:prod": "node dist/main",


* Some Hosting Providers use Procfile for running web applications, so create a file in the project folder named `Procfile` and add the following single line to it.
```procfile


web:npm run start:prod


Enter fullscreen mode Exit fullscreen mode
  • Delete the dist folder and run npm run start in command-line/terminal to create the latest version of compiled output files. (Deleting folder first is not necessary but it has proven helpful in some cases).
  • Push your changes to GitHub Repository using these three basic commands: ```bash

git add . //Stage All Files including dist folder
git commit -m "Commit Message"
git push

* Setup your cloud instance on any platform:
  * [AWS EC2 Instance](https://www.guru99.com/creating-amazon-ec2-instance.html)
  * [A2Hosting](https://www.a2hosting.in/kb/cpanel/cpanel-software/create-application-with-nodejs-selector)
* Clone the GitHub repository to the instance using the terminal.
* Target your server to launch `dist/main.js` if required to specify.

In this way, your NestJS app will be deployed. 
<hr>
> This process is attested and followed by developers of www.era-co.in

<hr>
Thanks for Reading!
*What should I post about next? Do tell me in the comments. Suggestions, improvements, and additions are invited.*

Enter fullscreen mode Exit fullscreen mode

Top comments (13)

Collapse
 
ghana7989 profile image
Pavan Chindukuri

Hey is there any free alternative for deployment?

Collapse
 
allanjeremy profile image
Allan N Jeremy • Edited

Hey, you can use Heroku with their free plan for a start.

Collapse
 
saralufi profile image
Sara Lufi

Sadly there is no more free plan in heroku :(

Thread Thread
 
thejscode profile image
Jaskirat Grewal

Yup that's true.

Try Vercel.

Thread Thread
 
saralufi profile image
Sara Lufi

There is no proper resource for deploying a NestJS application of Nx Monorepo

Thread Thread
 
korostelevm profile image
Michael Korostelev

Try Cyclic

Thread Thread
 
saralufi profile image
Sara Lufi

Is there any example for deploying NestJS application or any resource at all?

Thread Thread
 
saralufi profile image
Sara Lufi

Image description
I am keep getting this error

Thread Thread
 
zekejohn profile image
Zeke

I'm also trying to deploy a NestJS App @saralufi , did you ever get this to work? If not, what did you do? Any help / suggestions would really help me out, because i'm new to deploying apps and NestJS. Thanks!

Thread Thread
 
zekejohn profile image
Zeke

Ok so i tried deploying it and i am getting the same error as you :(
Image description

Thread Thread
 
zekejohn profile image
Zeke

I just got it to work! Removed some packages i didn't need and re wrote some of the code to use lighter weight dependencies, and now everything works!! πŸ₯³ πŸ₯³

Thread Thread
 
paddymac profile image
Paddy McCallan

What port are you using for your main.ts? await app.listen(process.env.PORT || 3000); I'm not sure if cyclic has a specific port or not. It builds fine but get this when I try to access my api via swagger - [CYCLIC] ERROR: No response from server on port 3000

Thread Thread
 
nazavo profile image
Naz πŸ₯‘

Found this guide on deploying NestJS on Vercel if anyone is wandering about a free option too - dev.to/leduc1901/how-to-deploy-you...