Deploying a Node application to Heroku, part 1

Learn how to set up a start script and environment variables in a Node application

In this, and successive articles, we will take a look at the steps involved in making a Node application available on the internet for all to use.
For our example we will use Heroku as a hosting environment. Other hosting services may have different settings, but the basic steps are usually similar.

Node environment

When we create a new Node application on Heroku, the service will set up a Node environment to deploy our app.

By default, Heroku will look for a start script inside package.json that instructs the service on how to start the application.
If we don't have this script already set up, we need to add it.
In the code below we have two scripts in the script section of our package.json file: server and start.

// package.json

  "scripts": {
    "server": "nodemon index.js",
    "start": "node index.js"       // used by Heroku
  },

server will be used for our local development work.
When we start our application in development with yarn server, we are actually running this server script, which in turn starts the application with nodemon.

Nodemon, as we have seen before, restarts the application every time we make changes to the files. This is perfect in development, where we need to constantly update files and add new features, because changes can be seen immediately.

The start script, instead, will be used by Heroku. It will start the application in production by calling Node and passing index.js to it, which is the entry point of our app.

Defining a port

Our application needs to run on a port, which we have defined locally in our examples so far as port 4000.
This is an arbitrary number, which works on our machine, but there's no guarantee that this same port number will be available on a shared production service like Heroku.

This means that we can't hard code any port number into our application. What we need to do instead is set up some sort of variable that will take care of this setting for us.

Heroku, and other hosting services, uses a concept called "environment variables" to define settings that are specific for each application hosted on their servers.

As it turns out, our application may run in different environments, like development, production, testing, staging, and so on.
Environment variables are variables set for the environment our application runs in and to keep things tidy and separate, we want to be able to use a different set of variables for each environment.

Node helps us in this case by making available to us an object called process. This object has a property called env which is a collection of all the environment variables available to our app.

Node environment variables

If you start a Node session and call process.env you will see all the environment variables defined for the running process. Here's a partial list of what is printed out by my console:

> process.env
{
  LESSCLOSE: '/usr/bin/lesspipe %s %s',
  GDM_LANG: 'en_US',
  COLORTERM: 'truecolor',
  USER: 'cesare',
  DESKTOP_SESSION: 'cinnamon',
  DEFAULTS_PATH: '/usr/share/gconf/cinnamon.default.path',
  PWD: '/home/cesare',
  SHELL: '/bin/bash',
  LANGUAGE: 'en_US',
  GDMSESSION: 'cinnamon',

  //  and on and on ...
}

By default, there's no PORT variable, but we can add it to process.env and use the value of this variable as our port number.
We'll see how to define and use the process.env.PORT in the next article.


Did you like this article? Share it with your friends.
I write daily about front-end and back-end web technologies.
You can receive all my articles in your inbox by subscribing to my newsletter. Just click the button below. No spam, just good, useful content. Guaranteed!


Follow me on Twitter