Deploying Node Applications to Heroku

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

Deploying Node Application to Heroku

This is a quick & minimalistic tutorial on how to move your local Node app to a public hosting service like Heroku.

Prerequisites

Before we start off deploying, you need to be aware of Git and how to go about it. You can find a tutorial here. (I am assuming you have Node installed)

If you are deploying an app for the first time ever, I highly recommend creating a dummy Node project & playing around with it.

Create a new folder named myNodeApp. CD inside the folder and run  npm init to start a new node project. Fill it up as you need. After that add a section to include the node version you wish to target (engines). The completed package.json would be

Next, create another file named server.js. This will be a simple Node app, just to test things. The completed server.js would be

To test the app, you can run  node server.js  and navigate to  http://localhost:1881.

Deploying to Heroku

Step 1 : Create a Heroku account here.

Step 2 : Install the Heroku toolbelt from here.

Step 3 : Open terminal/prompt inside the folder that you would like to deploy. Following the prerequisite, it would be the myNodeApp folder.

Step 4 : Login to heroku from command line.

In the terminal/prompt run

And you would see

Step 5 : Add a public ssh key.

In the terminal/prompt run

You can use the below as help

I already have a key, so I overwrote it. Passphrase is not necessary but recommended. Then run

  heroku keys:add

This will add the newly generated key to your Heroku Account. If everything goes well, you will receive an email too. If you are facing any issues refer this.

Step 6 : Create a Procfile

The contents of Procfile tell Heroku what exactly needs to be done after the the project is uploaded to their box. Create a new file named Procfile (no extensions) at the same level as package.json.

The contents will be the exact same command you use to launch the node app on your local machine but with a prefix like :

If you want, you can add a node module named forever. This will make sure that even if the application crashes due to “unknown reasons“, it restarts again. For that run

npm install forever --save

to add this to our package.json. Then our updated Procfile would be

Where 5 is max number of times the script re-runs.

Step 7 : Create a local repo

Back to terminal/prompt run

Step 8 : Deploying to Heroku

Back to terminal/prompt run

heroku create

if you want Heroku to provide a random name, else run

heroku create mysuperawesomenodeapp

If the app name is available, heroku will create a new repo & add that as remote repo to your current local repo. But unfortunately, I own this name, so you will receive a message that this is taken. You can keep trying till find the sub-domain name you like.

Finally run

git push heroku master

This will push the local git repo to the remote one. You will see plenty of logs, as to what is happening on the server. And then if everything goes well, you will receive the success message & a URL to the deployed app. The last few lines of the log would be

Once this is done, we need to scale our dynos. Run

heroku ps:scale web=1

Step 9 : Post Deployment

Open the deployed app

heroku open 

Check the logs

heroku logs

Status & Scale the dynamos

heroku ps

heroku ps:scale web=1

Run Heroku node from console

heroku run node

You can find some more advanced options here. You can also perform a few operations from app dashboard too.

Do remember that you can host only 5 apps & 1 dyno per app as part of the free tier.


Thanks for reading! Do comment.
@arvindr21

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • http://pulpoficcion.tumblr.com/ J.Balczar

    Thanks for sharing!! I followed all steps but i got an error when trying to push to heroku : ! Push rejected, no Cedar-supported app detected

    HINT: This occurs when Heroku cannot detect the buildpack

    to use for this application automatically.

    See https://devcenter.heroku.com/articles/buildpacks

    Any help?

  • mikeh

    Very nice summary. Just to be clear: Unverified accounts can create at most 5 apps. Verified accounts (which will still not incur charges as long as each app has only 1 dyno) can create no more than 100 applications. https://devcenter.heroku.com/articles/limits#other

  • SundaraPandyan SA

    Perfect! It really helps

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Great! Glad I could be of help!