Deploy Node JS app on Heroku

Deploy your rest api created using node js to Heroku.

Deep Patel
3 min readOct 30, 2021
Photo by Greg Rakozy on Unsplash

In this demo, we will be looking at how to deploy your cool NodeJS app to Heroku. I assume that you have node app ready on local machine. You can refer my previous blog to know more about node js and rest api.

Steps

  1. Create Procfile in your project base directory and add this line to the file web: npm start .
  2. Update your pakage.json file. Add the below code.
"engines": {
"node": "14.x"
},
"keywords": [
"node",
"heroku",
"express"
],
Final pakage.json file

3. Create an empty repo in Github.com

4. Open your project and initialise git with command git init.

5. Add files to your local git or staging area git add .

6. Commit changes with commit git commit -m “initial”

7. Link local repo to Github repo via remote git remote add origin https://github.com/deeppatel23/api-demo.git similar to this.

Copy the commands as suggested by github when you create new repo.

8. Push your changes to Github repo. with command git push origin {branch_name} (use cmd git branch -a to check current branch).

9. Open your Heroku account dashboard. Create a new app.

Heroku dashbord image

10. Give an appropriate name and create app.

11. Go to the deploy section and connect your Github account. You will see your account once it gets connected and now search for the repository. Select the repository and click connect.

Select appropriate repository and click connect.

12. Click Enable Automatic Deploys so that as soon as the changes are pushed to GitHub, Heroku will pick them up and deploy.

13. Add node js build pack from the Settings tab and save the changes.

14. Now, go back to the Deploy tab, and click Deploy Branch at the bottom.

15. Open the Activity tab and there you can see the progress. Open the settings tab and scroll down to the Domains and certificates section. Here, you can see the URL of your app that was just deployed. Copy and paste that URL in the browser and… Hoooorah!!

root: https://api-demo23.herokuapp.com
Follow all the above steps to deploy successfully. (https://api-demo23.herokuapp.com/books)

PS: If your projects runs successfully on local machine but give some application error on Heroku, then you must check Procfile, or Engines versionin package.json, or path of any other files listed (Note: path should start with ../{file_name} eg: “../api-demo-file.json” ). Do check logs (Dashboard -> More -> View Logs)for better understanding of errors.

--

--

Deep Patel
Deep Patel

Written by Deep Patel

Hi everyone, I am a software engineer at American Express. I am here to share my experience in tech.

No responses yet