How to Easily Deploy Your Node.Js App on Heroku

Hitesh Mandav
Geek Culture
Published in
5 min readOct 6, 2021
NodeJS+Heroku

As a beginner, we usually build multiple projects, apps but this is only one half of the puzzle. We also want the world to access these apps be it for a demo, potential recruiter or just to get it out in front of everyone to use and test our new crazy idea. And for this, we need to deploy these apps on a public server.

This article will show you how to deploy your first Node.js app easily to Heroku.

This is not a tutorial to help you learn Node.js. It is assumed you have completed development and are ready to deploy. However, we’ll set up a basic node js project from start and deploy.

This tutorial will cover:

  • Creating a sample Node.js application
  • Deploying sample Node.js application
  • Deployment using Heroku CLI
  • Automatic deployment using Git

Assuming you have node.js installed on your machine, follow the below steps.

Creating a sample Node.js application

Create a folder, open cmd go to the folder that you have created and run npm init this will create a package.json file. It will prompt you for several inputs including the name, version, etc of your application. For this example we will accept all the defaults.

Now you will have a package.json file.

//package.json
{
"name": "first-node-app",
"version": "1.0.0",
"description": "my first nodejs express app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

Now run the below command to install express as we will use this middleware to build our server.

npm install express --save

Now in your package.json add “start”: “node index.js” inside the scripts object.

Now create a server.js file and add the below code.

const express = require('express');
const app = express();

app.get('/', (req, res) => {
res
.status(200)
.send('Hello server is running')
.end();
});

// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});

Now you can test this application locally by running

npm start
Server running on browser

Create a file and name it Procfile in your project and copy the below line this will tell the Heroku server how to execute our project.

web: node server.js

Deploying sample Node.js application

Create a Heroku account for free here. If you already have an account then log in and go to the dashboard.

Here you need to create a new app.

Heroku Dashboard

And enter a unique app name

Heroku create new app

Deployment using Heroku CLI

You need to install Heroku CLI on your machine you can install it from here, and verify the installation by running heroku --version .

If you are facing any issue with the above installation method you can run npm install -g heroku command assuming you have npm and node installed and try to verify again.

Now once the installation is successful you need to go to your server-side project and run the below command in the terminal.

heroku login

This will either redirect you to the browser or prompt for Heroku login credentials in the terminal once you are logged in successfully you are all set to deploy your code.

Now initialize git repository inside your working directory and connect that to your Heroku app using the second command with your app name.

$ git init
$ heroku git:remote -a unique-app-name-to-be-entered

Now you can add and commit to the Heroku master branch using the following commands.

$ git add .
$ git commit -m "your commit message"
$ git push heroku master
deployed successfully

Once you are done you will be able to see that the build and deployment are successful in the latest activity section of the Heroku app overview. Now you can click the app button on the top to view your node application.

Automatic deployment using Git

Now the deployment is done but whenever you make changes in your code you will have to push your changes to Heroku git to deploy your changes. And you will most likely have a git hub repository where you commit your code already, and we don't want to do this extra work every time.

So to deal with this Heroku provides a solution you can connect your Github repository to your Heroku app and Heroku will do the deployment automatically whenever you push your changes to GitHub.

To do this, you can go to your Heroku app. Under the deploy section, you need to select the second option GitHub. ( If your Heroku account is not connected to git hub you will have to log in to Github and connect your GitHub account.) and you will be able to see an input box to search the repository associated with your git hub account. You can look for your repo and click connect.

Connect to git hub repo

Once the repository is connected you can select enable automatic deploys and you are all done now whenever there is a push to your Github repo Heroku will automatically deploy the changes.

enable automatic deployment

Thank you for the read and do comment below if you encountered any issue or want to suggest better ways.

--

--