Deploying Node.js app on Heroku

If you want to test or distribute your node.js application then Heroku Cloud Platform is a good option. Heroku provides you a free cloud storage with some limited capabilities (of course). Heroku provides free cloud platform for Node.js, PHP, Python and Ruby. In this article I will tell you how to deploy your node.js application on Heroku.

Set up

I am sure you have some basic node.js app running on your localhost. So go to Heroku site and download the Heroku Toolbelt for you operating system. Heroku Toolbelt is a command line utility which lets you perform all the deployment related processes. You also need an account on Heroku.
Demo App on Heroku

Step 1: Login on Heroku

Open the command window and type:

$ heroku login

Login with your heroku credentials.

Step 2: Create Git repo

You can deploy only the valid git repos on Heroku. So either you can clone any git repo or just initiate git in your local directory.

$ git clone
$ cd IonicOfflineBlog


$ git init

Step 3: Package.json

If you are deploying a node.js app to heroku, it is necessary that your app should have package.json at the root of project. By reading this package.json heroku will identify your node.js app. Heroku will automatically run the npm install command to install the app dependencies specified in package.json.

Step 4: Create Procfile

Procfile is heroku specific file that needs to be there along with package.json. Create the Procfile without any extension. Procfile contains the start command for your node.js application. In simple apps this command is starting the node server. Here is the sample Procfile for node.js app:

web: node server.js

The term web: is mandatory. server.js should be present at your project root. If your project has statick .html, .css or .js files to be served, then you need to specify this in server.js. Here is the sample server.js:

// server.js
var express = require('express');
var app = express();
var path = require('path');

// "www" is considered as the public folder in your app to serve the static resources
app.use(express.static(path.join(__dirname, 'www')));

// This is necessary to specify the port from process.env
// Otherwise heroku will throw an error
app.listen(process.env.PORT || 5000);
console.log("Application server started...");

Step 5: Deploy the App

Now you need to create the app on heroku as:

$ heroku create <app-name>

app-name is not necessary. You can rename an app using:

$ heroku apps:rename <new-name>

Now you can commit all the changes in your app using git commit and push them to heroku:

$ git push heroku master

This command will ask you for credentials. Do not enter anything in the username field (just hit Enter to proceed to password field).
Password is the auth token. You need to generate the auth token using this command:

$ heroku auth:token

This command will generate a long string. Use this string as password in above step and you will be in.

It will take a while to deploy the app. After the sucess message you need to run the dynos on heroky cloud:

$ heroku ps:scale web=1

This will start one dynos for your app. read more about dynos.
Now run following command to run the app in browser:

$ heroku open

Step 6: Important Points

See the log:

$ heroku logs --tail

Run node console:

$ heroku run node

Run Bash

$ heroku run bash

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.


One thought on “Deploying Node.js app on Heroku

Comments are closed.