Deploying a NodeJS app to Heroku
Tags: node, deploy, beginners, api
Published: 2020-02-10 16:00:42

---
title: Deploying a NodeJS app to Heroku
published: true
description: Learn step-to-step how to deploy a NodeJS app to Heroku
tags: #node #deploy #beginners #api
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ts7q5u3h7zw7rgz1dba5.jpg
---

Hello there! Some time ago I've made a [post](https://dev.to/raymag/build-a-quiz-rest-api-with-nodejs-2p64) about how to build a quiz REST API with NodeJS. Now we are going to **deploy** it to [heroku](https://www.heroku.com/), for free. Heroku is a platform that allows developers to build, run and operate applications on the cloud. They also supports a bunch of languages, like python, php, go, java, etc.

If you just arrived here and doesn't have any kind of node app to deploy, I'd recommend you to take a look at my [post](https://dev.to/raymag/build-a-quiz-rest-api-with-nodejs-2p64) about building a node API or cloning the [repository](https://github.com/raymag/quiz-node-api). That way you can practice as we go.

## Table of Contents
- [Creating a account](#creating-a-account)
- [Setting up a new app](#setting-up-a-new-app)
- [Setting up package-json](#setting-up-package-json)
- [Deploying](#deploying)
- [Testing](#testing)
- [Conclusion](#conclusion)

## Creating a account

That being said, first create a account on [heroku](https://signup.heroku.com/). After making and verifying your account, you'll be redirected to the dashboard.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vgtbm252pndja91k109o.png)

This account of mine isn't new, that's why I've already got a few apps deployed. But yours will probably be empty. 

## Setting up a new app

Alright! Let's click on the *New* button at the top-right corner of the page and then create a new app. Give it a cool name and select the proper server region depending on your location.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d0frtztkacld0o3aftwc.png)

You'll now be redirected to the deploy section of the app. You can choose the best deploy method for you, the instructions for using it will be right below. But in my case, I'll be choosing the Github method, since I've already have a github repository for the app and it'll allow me to enable automatic deploy afterwards.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ndxc2qy1gpzxobylr1lk.png)

I've connected the heroku app to the github repository. We can now make a manual deploy or enable automatic deploy. By enabling that, every time we make a new push or merge in the repository, it'll automatically deploy to heroku. That actually save us lots of time, so of course we're enabling it!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wjne6bplp188979dba1o.png)

## Environment Variables

Since our app uses a few environment variables like the PORT number and DATABASE_URL, we have to set up these variables in Heroku. For that purpose, we can open up the settings tab in the dashboard.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p3tvbcjh8j9jjh6a4pqe.png)

Let's now click on the *Reveal Config Vars* button. You'll now be able to add new variables. For the API we've built before, you'll just going to need the DATABASE_URL. The PORT number environment variable is provided by Heroku by default.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jrqcrror4a8w7fb239mr.png)

## Setting up package json

Before we actually deploy, we have to configure our app. When deploying to Heroku, we can either use a *[Procfile](https://devcenter.heroku.com/articles/procfile)* or the *package.json* for settings. Since we'll not use a Procfile, we'll have to guarantee that our *package.json* has a start script. That have to be done because when Heroku doesn't find a *Procile* it'll try to use the start script in package.json.

In our case, the script key in package.json will be something like this:
```json
  "scripts": {
    "start": "node src/server.js",
    "dev": "nodemon src/server.js"
  }
```

## Deploying

Now that we've configured everything, we can manually deploy or make a new push to our github repository. You can then take a look at the overview tab on the app's dashboard. If you see something like that, it probably means that everything is properly working.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fe1smkps3z6l63cokvia.png)

## Testing

It's now the time to test our deployed app. If everything went well, you can click on *Open app* in the dashboard or access it with the URL. It should be something like the *name_of_the_app.herokuapp.com*.

Here's a picture from me testing one of the routes from our deployed API in the browser.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/casekbqka6m1aya3clc4.png)

## Conclusion

So that's how we deploy a node app to heroku! If you have any questions you can either leave comments here or read the [documentation](https://devcenter.heroku.com/articles/deploying-nodejs).

Thank you for reading until here, if you have any suggestions or corrections as well, I'd be really glad to know. See ya!