0
0
NodejsHow-ToBeginner · 4 min read

How to Deploy Node.js to Heroku: Step-by-Step Guide

To deploy a Node.js app to Heroku, first create a Heroku app using the CLI, then push your code with git push heroku main. Ensure your app has a Procfile and package.json with a start script for Heroku to run it correctly.
📐

Syntax

Deploying Node.js to Heroku involves these main commands and files:

  • heroku create: Creates a new Heroku app linked to your local git repo.
  • git push heroku main: Pushes your code to Heroku for deployment.
  • Procfile: Tells Heroku how to start your app, usually web: node index.js.
  • package.json: Must include a start script to launch your app.
bash
heroku create your-app-name
# Creates a new Heroku app

git push heroku main
# Pushes your code to Heroku

# Procfile content:
web: node index.js

# package.json start script example:
"scripts": {
  "start": "node index.js"
}
💻

Example

This example shows a simple Node.js app deployed to Heroku. It includes a package.json with a start script, a Procfile, and the commands to deploy.

plaintext
/* index.js */
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('Hello from Heroku!');
});

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

/* package.json */
{
  "name": "heroku-node-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  }
}

/* Procfile */
web: node index.js

/* Deployment commands */
# Initialize git if not done
# git init
# git add .
# git commit -m "Initial commit"

heroku create your-app-name

git push heroku main
Output
-----> Node.js app detected -----> Installing dependencies -----> Build succeeded -----> Discovering process types Procfile declares types -> web -----> Compressing... -----> Launching... Released v1 https://your-app-name.herokuapp.com/ deployed to Heroku Server running on port 3000
⚠️

Common Pitfalls

Common mistakes when deploying Node.js apps to Heroku include:

  • Missing Procfile or incorrect command inside it.
  • Not specifying a start script in package.json.
  • Hardcoding the port instead of using process.env.PORT.
  • Forgetting to commit changes before pushing to Heroku.
  • Not installing dependencies properly or missing package-lock.json.
plaintext
/* Wrong Procfile example (missing 'web:' prefix): */
node index.js

/* Correct Procfile example: */
web: node index.js

/* Wrong start script in package.json: */
"scripts": {
  "start": "node app.js"
}

/* Correct start script (matches your main file): */
"scripts": {
  "start": "node index.js"
}
📊

Quick Reference

Summary tips for deploying Node.js apps to Heroku:

  • Always use process.env.PORT for the port.
  • Include a Procfile with web: node your-main-file.js.
  • Make sure package.json has a start script.
  • Commit all changes before git push heroku main.
  • Use heroku logs --tail to see app logs if issues arise.

Key Takeaways

Use a Procfile and a start script in package.json to tell Heroku how to run your app.
Always listen on process.env.PORT, not a fixed port number.
Commit your code before pushing to Heroku with git push heroku main.
Use heroku create to set up your app and heroku logs --tail to debug.
Ensure all dependencies are listed in package.json and installed.