How to Deploy Your Node.Js App to Heroku.

How to deploy your Node.js API to Heroku in 5 steps.

Moses Esan
The Startup
Published in
2 min readNov 23, 2019

--

In this tutorial, I will be laying out the steps required to deploy a Node.js app to Heroku. For this example, I will be deploying the authentication API built in my previous tutorial.

Requirements

  1. Heroku account, sign up for free
  2. The Heroku CLI
  3. GitHub account

Setup GitHub Repo

If your project is not set up to use git, run the commands below

Initialize a git repository in a new or existing directory$ cd nodejsapi
$ git init
$ touch .gitignore
---Update the .gitignore filenode_modules/**/*
npm-debug.*
.env
---$ git add .
$ git commit -m "Committing to Deploy to Heroku"

Step 1: Add Procfile

Add a Procfile to the root directory to declare the command to be executed to start the server.

Make sure you are in the root directory.$ touch Procfile

Update the Procfile with the code below. This declares a single process type web and the command needed to run it.

web: node src/server.js

Commit your changes

$ git add .
$ git commit -m "Added a Procfile."

Step 2: Log in to your Heroku account

$ heroku login

Step 3: Create a Heroku App

$ heroku create mesannodejsapiWhen you create an app, a git remote (called heroku) is also created and associated with your local git repository.Run the command below to confirm that a remote named heroku has been set for your app$ git remote -v

Step 4: Set Environment Variables

Run the commands below to set the environment variables

heroku config:set JWT_SECRET=[.....]
heroku config:set MONGO_LOCAL_CONN_URL=[.....]
heroku config:set CLOUD_NAME=[.....]
heroku config:set CLOUD_API_KEY=[.....]
heroku config:set CLOUD_API_SECRET=[.....]
heroku config:set SENDGRID_API_KEY=[.....]
heroku config:set FROM_EMAIL=[.....]

Step 5: Deploy

Run the code below to deploy your app

$ git push heroku master

The application is now deployed. Ensure that at least one instance of the app is running:

$ heroku ps:scale web=1

Now visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows:

$ heroku open

TROUBLESHOOTING

If for some reason, something went wrong, use the command below to check the log.

$ heroku logs --tail

That’s all folks!

That’s all folks!

Related Tutorials

  1. How to Build a Node.js Authentication API with Email Verification, Image Upload and Password Reset Using JWT, Passport.js, and Sendgrid.

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Moses Esan
Moses Esan

No responses yet