DEV Community

Cover image for Deploy NodeJS applications to AWS or HEROKU
GNU/Linux Users' Group, NIT Durgapur

Deploy NodeJS applications to AWS or HEROKU

vrohan profile image V. Rohan Rao Updated on ・6 min read

The Premise.

NodeJS revolutionised the back-end architecture worldwide. Ryan Dahl made server side Javascript possible, further strengthening the position of JavaScript as an indelible part of the Web as we know it today.


Starting at the grassroots level, often first-timers have to deploy their code to a production server, say for a Hackathon or some freelance work.

Heroku and AWS are the best choices for deployment right now, with their free plans nicely covering most of the needs for a first timer.

Yet, deployment is a maze of it's own. Procfiles, environment variables, proxy settings, port exposing, it's a dire stretch for a person trying to spin up a server for the first time.

This article aims at helping you out to deploy your NodeJS apps.

But the bigger question is where to deploy?

What exactly are the differences between Heroku and AWS? And what are the benefits of choosing one over the other?

Heroku vs AWS

Heroku is container-base cloud platform offering (PaaS) whereas AWS is a secure cloud services platform providing IaaS, PaaS and SaaS.

(Infrastructure/Platform/Software as a Service)

Heroku is a easy to go solution for deploying if you are a beginner. You don’t have to actually worry about the infrastructure and scalability.

AWS in the beginning can be very complicated but gives you more control on the infrastructure of your site. So if you are beginner it’s better to try Heroku than configuring so many things in the AWS instance when you can get one step deployment solution by using Heroku.

So, let's begin, shall we?

Deploying NodeJS Apps on Heroku

Heroku Logo

We will use Heroku CLI for deploying.

Download Heroku CLI from here.

After successfully installing the heroku cli run the following command:

heroku login
Enter fullscreen mode Exit fullscreen mode

Change the directory to the project folder.

Specifying a start script

To determine how to start your app, Heroku first looks for a Procfile. If no Procfile exists for a NodeJS app, we will
attempt to start a default web process via the start script in your package.json.

What is a Procfile? More on that below.

The command in a web process type must bind to the port number specified in the PORT environment variable. If
it does not, the dyno will not start.

What is a dyno?

All Heroku applications run in a collection of lightweight Linux containers called dynos.

~ Heroku DEV center

If you hardcode the port in your code, the deployment will fail.

Make sure you use process.env.PORT in your code.

Add a Procfile

What is a Procfile?

Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. You can use a Procfile to declare a variety of process types

~ The Heroku DEV center

The Procfile, is a list of custom commands which are defined by certain keywords like web or worker.

These are steps and commands which have to be executed each time your app is started. Heroku executes all the processes in your Procfile in different dynos, light-weight linux containers, which connect among each other.

Create file with name Procfile (no extension) and add the line.

web: node index.js
Enter fullscreen mode Exit fullscreen mode

Build your app and run it locally

  • Run the 'npm install' command in your local app directory to install the dependencies that you declared in your package.json file.
npm install
Enter fullscreen mode Exit fullscreen mode
  • Start your app locally using the Heroku local command, which is installed as part of the Heroku CLI.
heroku local web
Enter fullscreen mode Exit fullscreen mode

Your app should now be running on http://localhost:5000/.

Deploy your application to Heroku

After you commit your changes to git, you can deploy your app to Heroku.

git add .
Enter fullscreen mode Exit fullscreen mode
git commit -m "Added a Procfile."
Enter fullscreen mode Exit fullscreen mode
heroku login
Enter fullscreen mode Exit fullscreen mode

Enter your Heroku credentials.

$ heroku create
Creating arcane-lowlands-8408... done, stack is cedar |
Git remote heroku added
Enter fullscreen mode Exit fullscreen mode
git push heroku master
-----> Node.js app detected
-----> Launching... done deployed to Heroku
Enter fullscreen mode Exit fullscreen mode

To open the app in your browser, type-

heroku open.
Enter fullscreen mode Exit fullscreen mode

Deploy NodeJS on AWS


As a very first step, We need to create a EC2 instance on the AWS, with Amazon Linux 2 as the OS of choice for your server, and ssh into it .

After successful ssh into the EC2 instance , we need to follow the following steps to successfully deploy the NodeJS application.

  1. Install NodeJS and NPM
  2. Install Git and clone the repository with your project files.
  3. Install all the dependencies
  4. Install pm2 to run the server as a background process configure the security group for the Ec2 instance

Step 1 – Install NodeJS and NPM

Install NVM (Node Version Manager) by running the following command.

curl -o- | bash
Enter fullscreen mode Exit fullscreen mode

Activate nvm by typing the following at the command line

. ~/.nvm/
Enter fullscreen mode Exit fullscreen mode

Use nvm to install the latest version of NodeJS by typing the following at the command line.

nvm install node
Enter fullscreen mode Exit fullscreen mode

Installing NodeJS also installs the Node Package Manager (NPM) so you can install additional modules as needed.

Test that NodeJS is installed and running correctly by typing the following at the command line.

node -v
Enter fullscreen mode Exit fullscreen mode
npm -v
Enter fullscreen mode Exit fullscreen mode

Step 2 – Installing Git and cloning the repository

To install Git , run the following command in the terminal

sudo yum install git
Enter fullscreen mode Exit fullscreen mode

To verify whether Git was installed properly in the system or not run the following command.

git –version
Enter fullscreen mode Exit fullscreen mode

Run the following command to clone the repository

git clone <repository link>
Enter fullscreen mode Exit fullscreen mode

Step 3 – Installing the dependencies

change the directory to the cloned repo folder and run the following command.

npm install
Enter fullscreen mode Exit fullscreen mode

Step 4 – Installing PM2 and starting the server

PM2 is a daemon process manager that will help you manage and keep your application online.

Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.

A daemon (pronounced DEE-muhn) is a program that runs continuously and exists for the purpose of handling periodic service requests that a computer system expects to receive. The daemon program forwards the requests to other programs (or processes) as appropriate.


A Daemon process is a program which runs as a background process, without the need of user interaction via terminal or a GUI. Running the node app as a daemon process enables you to access and execute other processes from your SSH session on your server, without which your node process will take up your current session's terminal.

To install pm2 run the following command.

sudo npm install pm2 -g
Enter fullscreen mode Exit fullscreen mode

To start the server run:

sudo pm2 start build/index.js
Enter fullscreen mode Exit fullscreen mode

To delete a pm2 process run

sudo pm2 delete <index no of the process>
Enter fullscreen mode Exit fullscreen mode

Step 5 - Configure Security groups

By default, nobody can access the application without configuring the Inbound traffic configurations for the EC2 instance.

Quick explanation

The external IP dedicated to your EC2, only looks for SSH requests by default. Normal webpages, and REST APIs, mostly use the TCP protocol for data transfer. To make sure your server is secure and is not vulnerable to DDoS attacks (Distributed Denial Of Service), and to make sure that your assets and data remains secure.

To make sure that front-end applications can send requests and receive responses from our NodeJS application, we will need to expose the port to incoming TCP connections by changing the security settings for our EC2 instance.

To configure Inbound traffic for the EC2 instance, follow the below steps:

  1. Select the EC2 Instance and click on the security group link in the Description section.

  2. By clicking the security group it will open the Security Group section. Here, we can configure Inbound and Outbound traffic properties. To make our application accessible from anywhere, click on the Inbound tab.

  3. By clicking on the Edit button available in the Inbound tab, it will open Edit Inbound rules popup. By default, it will show SSH configurations.

Since our application is configured for port number as your required port, we need to add a new rule “Custom TCP Rule”.

Enter port range as your required port and select Source as “Anywhere”. After saving the changed rules, it will allow us to access our application from anywhere.

That's it, folks!🎊 You've learnt successfully how to deploy your NodeJS application to production!

We hope you found this insightful.
Do visit our website to know more about us and also follow us on :

Also do not forget to like and comment.

Until then,
stay safe, and May the Source Be With You!

Star Wars Who?

This article was co-written by

vrohan image


deadlycoder07 image

Discussion (0)

Editor guide