DEV Community

Cover image for Deploying NestJS Apps to Heroku: A Comprehensive Guide
Ezile Mdodana
Ezile Mdodana

Posted on

Deploying NestJS Apps to Heroku: A Comprehensive Guide

Deploying a NestJS application to Heroku can be a straightforward process if you follow the right steps. In this guide, we'll walk you through the entire process, from setting up your NestJS application to deploying it on Heroku and handling any dependencies or configurations.

Step 1: Setting Up Your NestJS Application

Before deploying, ensure you have a NestJS application ready. If you don't have one, you can create a new NestJS project using the Nest CLI.

1. Install Nest CLI:

npm install -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode

2. Create a New Project:

nest new my-nestjs-app
cd my-nestjs-app
Enter fullscreen mode Exit fullscreen mode

3. Run the Application Locally:

npm run start:dev
Enter fullscreen mode Exit fullscreen mode

Step 2: Prepare Your Application for Heroku

Heroku requires a few specific configurations to properly deploy a Node.js application.
Let's go through these configurations step by step.

1. Create a Procfile:

In the root of your project, create a file named Procfile and add the following line:

web: npm run start:prod
Enter fullscreen mode Exit fullscreen mode

This tells Heroku to start your application using the production build.
NB: The name of the file should be Procfile, nothing more nothing less, if the case is different from this, heroku won't recognize the file.

2.Update package.json:
Ensure your package.json has the following scripts:

"scripts": {
  "start": "nest start",
  "start:dev": "nest start --watch",
  "start:prod": "node dist/main.js",
  "build": "nest build"
}
Enter fullscreen mode Exit fullscreen mode

Additionally, add the following configuration to ensure your app runs on the correct port:

"engines": {
  "node": ">=14.0.0"
}
Enter fullscreen mode Exit fullscreen mode

Depending on the node version you are using for your application.

Step 3: Initialize a Git Repository

Heroku uses Git for deployments. Initialize a Git repository if you don't have one already.

1.Initialize Git:

git init
git add .
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy to Heroku
Now, let's deploy your application to Heroku.

1. Login to Heroku:
Let's use the Heroku CLI to login, run the command on your project directory.

heroku login
Enter fullscreen mode Exit fullscreen mode

This command will open a browser and require your login credentials, populate and complete 2FA, if enabled. The come back to the CLI.

2. Create a New Heroku Application:

heroku create my-nestjs-app
Enter fullscreen mode Exit fullscreen mode

3. Deploy Your Application:

git push heroku master
Enter fullscreen mode Exit fullscreen mode

Step 5: Handle Environment Variables

Heroku allows you to manage environment variables through its dashboard or CLI, let's manage a few via the CLI.

1. Set Environment Variables:

heroku config:set NODE_ENV=production
Enter fullscreen mode Exit fullscreen mode

If you have other environment variables, set them as well:

heroku config:set DATABASE_URL=your_database_url
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Your Deployment

1. Open Your Application:

heroku open
Enter fullscreen mode Exit fullscreen mode

You can open the application from the Heroku dashboard as well.

2. Check Logs:

If you encounter any issues, check the logs with:

heroku logs --tail
Enter fullscreen mode Exit fullscreen mode

Conclusion

Deploying a NestJS application to Heroku is a straightforward process if you follow these steps. By preparing your application properly and using Heroku's powerful features, you can have your NestJS app running in the cloud in no time. Whether you're deploying a simple API or a complex application with multiple dependencies, Heroku provides a robust platform for managing your applications. Happy coding!

My way is not the only way!

Top comments (0)