Pages

Monday, 30 November 2020

Deploy Your Code to Heroku: Complete Guide (GitHub & Node.js)

Heroku provides a smooth workflow for deploying apps directly from GitHub or via Git. In this guide, you’ll learn both methods step-by-step.

Deploying applications to Heroku is simple once you understand the process. This guide covers:

✔ How to deploy an app to Heroku directly from GitHub
✔ How to deploy a Node.js app to Heroku using Git
✔ Important commands and setup instructions

1. How to Deploy an App to Heroku from GitHub

Follow the steps below:

Step 1: Open Heroku

Login to your Heroku dashboard: https://dashboard.heroku.com/

Step 2: Create a New App

  • Click New → Create new app
  • Enter your app name (example: test)
  • Click Create app

Step 3: Connect to GitHub

  • Go to the Deploy tab
  • Select GitHub under Deployment Method
  • Authorize Heroku (first time only)

Step 4: Select Your GitHub Repository

  • Search repo name
  • Click Connect

Step 5: Enable Auto Deploy (Recommended)

Click Enable Automatic Deploys

Step 6: Deploy the Branch

  • Select branch (main or master)
  • Click Deploy Branch

Step 7: Your App Is Live

You will receive your live URL, for example:
https://test.herokuapp.com


2. How to Deploy a Node.js App to Heroku (Using Git)

Step 1: Create a New App in Heroku

Same as earlier — create a new app in Heroku.

Step 2: Open Terminal

  • VS Code Terminal
  • OR Git Bash in project directory

Step 3: Login to Heroku

heroku login

Step 4: Create a Procfile

touch Procfile

If index.js is in main folder:

web: node index.js

If index.js is inside /src folder:

web: node ./src/index.js

Step 5: Initialize Git

git init

Step 6: Add Heroku Remote

heroku git:remote -a <your-app-name>

Step 7: Push Code to Heroku

git add .
git commit -m "initial deploy"
git push heroku master

Your app will be deployed. You will get your live URL immediately.


Updating the App (Later Changes)

Whenever you update code, run:

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

Your application will update automatically.


Final Notes

  • Ensure app has a valid package.json
  • Include a proper start script:
"start": "node index.js"
  • Add environment variables in Heroku → Settings → Config Vars

-- Thanks --

No comments:

Post a Comment

Deploy Your Code to Heroku: Complete Guide (GitHub & Node.js)

Heroku provides a smooth workflow for deploying apps directly from GitHub or via Git. In this guide, you’ll learn both methods step-by-step....