codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

38 / 76 topics
35Deployment Strategies for Express.js Applications36Dockerizing Express.js Applications37Deploying to AWS with Elastic Beanstalk38Deploying to Heroku
Tutorials/Express.js/Deploying to Heroku
🚂Express.js

Deploying to Heroku

Updated 2026-05-15
10 min read

Deploying to Heroku

Introduction

Heroku is a cloud platform that supports multiple programming languages and frameworks, including Node.js. Deploying an Express.js application to Heroku can be a straightforward process once you have your application ready for production. This tutorial will guide you through the steps required to deploy your Express.js application to Heroku.

Concept

Deploying an application to Heroku involves several key steps:

  1. Prepare Your Application: Ensure your application is ready for deployment by setting up environment variables, configuring a Procfile, and ensuring all dependencies are listed in your package.json.
  2. Create a Heroku Account: If you don't already have one, sign up for a Heroku account.
  3. Install the Heroku CLI: This command-line tool allows you to interact with Heroku directly from your terminal.
  4. Initialize Your Git Repository: Heroku uses Git for deployment, so make sure your application is in a Git repository.
  5. Deploy Your Application: Use the Heroku CLI to push your code to Heroku.

Examples

Step 1: Prepare Your Application

Before deploying, ensure your Express.js application is ready for production. This includes setting up environment variables and configuring a Procfile.

Setting Up Environment Variables

Create a .env file in the root of your project to store environment variables:

plaintext
1.env
2PORT=3000
3DB_HOST=localhost
4DB_USER=root
5DB_PASS=s1mpl3

You can use packages like dotenv to load these variables into your application:

JavaScript
1// app.js
2require('dotenv').config();
3
4const express = require('express');
5const app = express();
6const port = process.env.PORT || 3000;
7
8app.get('/', (req, res) => {
9res.send('Hello World!');
10});
11
12app.listen(port, () => {
13console.log(`App listening at http://localhost:${port}`);
14});

Creating a Procfile

Heroku uses the Procfile to determine how to run your application. Create a file named Procfile in the root of your project:

plaintext
1web: node app.js

Step 2: Create a Heroku Account

If you don't have a Heroku account, sign up at Heroku's website.

Step 3: Install the Heroku CLI

Download and install the Heroku CLI from Heroku's official site. Once installed, log in to your Heroku account using the following command:

Terminal
$ heroku login
Enter your Heroku credentials.
Email: user@example.com
Password (typing will be hidden):
Authentication successful.

Step 4: Initialize Your Git Repository

Ensure your application is in a Git repository. If it's not, initialize one with the following commands:

Terminal
$ git init
Initialized empty Git repository in /path/to/your/app/.git/
$ git add .
$ git commit -m "Initial commit"
[master (root-commit) abc1234] Initial commit
5 files changed, 100 insertions(+)
create mode 100644 .env
create mode 100644 Procfile
create mode 100644 app.js
create mode 100644 package.json
create mode 100644 package-lock.json

Step 5: Deploy Your Application

Create a new Heroku app and push your code to it:

Terminal
$ heroku create my-express-app
Creating ⬢ my-express-app... done
https://my-express-app.herokuapp.com/ | https://git.heroku.com/my-express-app.git
$ git push heroku master
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (10/10), 2.35 KiB | 2.35 MiB/s, done.
Total 10 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment...
remote:
remote: NPM_CONFIG_LOGLEVEL=error
remote: NODE_ENV=production
remote: NODE_MODULES_CACHE=true
remote: NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote: engines.node (package.json): unspecified
remote: engines.npm (package.json): unspecified
remote: Resolving node version (latest stable) via semver.io...
remote: Downloading and installing node 16.13.0...
remote: Using default npm version: 8.1.4
remote:
remote: -----> Installing dependencies
remote: Installing node modules (package.json)
remote:
remote: added 50 packages, and audited 51 packages in 2s
remote:
remote: 3 packages are looking for funding
remote: run `npm fund` for details
remote:
remote: found 0 vulnerabilities
remote:
remote: -----> Build
remote: Preparing package.json dependencies for caching
remote: Done caching npm dependencies
remote:
remote: -----> Caching build
remote: Clearing previous node cache
remote: Saving 251.7M cache layer
remote:
remote: -----> Launching...
remote: Released v3
remote: https://my-express-app.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/my-express-app.git
* [new branch] master -> master

Once the deployment is complete, you can open your application in a browser:

Terminal
$ heroku open
Opening https://my-express-app.herokuapp.com/ in your default browser...

Step 6: View Logs

You can view logs for your Heroku app using the following command:

Terminal
$ heroku logs --tail
2023-10-05T14:28:37.123Z app[web.1]: Express server listening on port 3000
2023-10-05T14:29:00.456Z app[web.1]: GET / 200 1ms - 12b

What's Next?

After deploying your Express.js application to Heroku, you might want to monitor its performance and health. You can use Heroku's built-in monitoring tools or integrate third-party services like New Relic or Datadog for more detailed insights.

Heroku also provides features like dynos scaling, which allows your application to handle varying loads by automatically adjusting the number of running instances.


PreviousDeploying to AWS with Elastic BeanstalkNext Monitoring Deployed Express Applications

Recommended Gear

Deploying to AWS with Elastic BeanstalkMonitoring Deployed Express Applications