Deploying a Node.js App to Heroku: A Step-by-Step Guide

Deploying a Node.js App to Heroku: A Step-by-Step Guide

Effortlessly Launch Your Node.js App on Heroku: A Comprehensive Guide


Heroku is a cloud platform that enables developers to deploy, manage, and scale applications. It supports several programming languages, including Node.js, making it a popular choice for hosting Node.js applications. In this guide, we will walk you through the process of deploying a Node.js app to Heroku.

Step 1: Prerequisites

Before you begin, make sure you have the following prerequisites:

  1. Node.js and npm installed on your local machine.

  2. A Heroku account. Sign up at Heroku.

  3. The Heroku CLI installed. You can download it from the Heroku CLI page.

Step 2: Create a Node.js App

If you don't already have a Node.js app, you can create a simple one for this guide. Create a new directory for your app and initialize a new Node.js project:

mkdir my-node-app
cd my-node-app
npm init -y

Create an index.js file with a simple HTTP server:

const http = require('http');

const PORT = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');

server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);

Step 3: Prepare the App for Deployment

Create a Procfile in the root of your project. This file tells Heroku how to run your app:

echo "web: node index.js" > Procfile

Step 4: Initialize Git Repository

If you haven't already initialized a Git repository for your project, do so now:

git init
git add .
git commit -m "Initial commit"

Step 5: Login to Heroku CLI

Login to the Heroku CLI using the following command:

heroku login

Step 6: Create a Heroku App

Create a new Heroku app using the Heroku CLI:

heroku create

This will create a new Heroku app and add a new Git remote called heroku.

Step 7: Deploy the App to Heroku

Deploy your app to Heroku using Git:

git push heroku master

Step 8: Open the App

Open your deployed app in your browser:

heroku open

Step 9: View Logs

View the logs of your app to check for any errors:

heroku logs --tail

Step 10: Scale the App

You can scale your app by adjusting the number of dynos (containers) running your app:

heroku ps:scale web=1

Congratulations! You have successfully deployed a Node.js app to Heroku. You can now continue to develop your app and deploy updates using the same process.

This guide provides a step-by-step process for deploying a Node.js application to Heroku, covering everything from setting up your environment and creating a simple Node.js app, to deploying and managing the app on Heroku's cloud platform.

Did you find this article valuable?

Support ByteScrum Technologies by becoming a sponsor. Any amount is appreciated!