Thursday, September 29, 2016

How to push node.js app to heroku

1) Download and install Heroku CLI. (For Mac :

2) Verify your installation
- heroku --version

3) Create free heroku account by visiting

4) Login to heroku from command line using the account that you have created above.
- heroku login (Enter registered email id and password)

5) Node, NPM and Git should be installed.

6) Create a sample Node.js app

i) Initialize (It will create package.json file)

- mkdir sample-node-project
- npm init (Follow the steps and enter few details )

ii) Install express.js

- npm install --save express

Check the package.json file, express.js should be added as dependancy. 

sample-node-project raja$ cat package.json 
  "name": "sample-node-project",
  "version": "1.0.0",
  "description": "test-example",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [
  "author": "praja",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0"

iii) Create Server

- sample-node-project raja$ vi index.js
- Add the following line into index.js

var express    =    require('express');
var app        =    express();
var server     =    app.listen(3000,function(){
    console.log("We have started our server on port 3000");

* Update values for "scripts" key as following in package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"

iv) Start the server

- npm start

v) Open the browser and type http://localhost:3000
- A blank page ( Cannot GET / ) will be displayed as we haven't set any route yet.

vi) Set the Router 

Add the following line before server start.

   res.send('Hello world');

vii) Stop and Start the server again.
viii) Go to http://localhost:3000
You should see "Hello World". As the router is set, so server is responding with "Hello world" response. 

7) Push the existing node.js project to GIT.

i) git init
ii) git status

iii) create .gitignore file add the following lines.
# Node build artifacts

iv) git status
v) git add .
vi) git commit -m "First Commit - Node Sample"

vii) Create a new repository in Check the repository URL. (For ex:

viii) git remote add origin "remote repository url"

ix) git push origin master

Final Changes before pushing node.js project to Heroku

8) Add node.js engine details in package.json so that both local and heroku environment will be on sync.

- Modify package.json, add node.js engine details (you can get the version by typing "node -v" in terminal)

"engines": {
    "node": "6.3.0"

9) Specify a start script for heroku. 

Heroku first looks for a Procfile. If no Procfile exists for the app, it will attempt to start a default web process via the start script in package.json.

Create a file name called "Procfile". 
Add the below content.

web: node index.js 

10) We can not hardcode the server listening port (local env : 3000) for heroku. So modify the app listen code in index.js with the following.

var server     =    app.listen(process.env.PORT || 3000,function(){
    console.log("We have started our server on port 3000");

11) Build the project and run it locally and load the browser with http://localhost:3000

App should run properly. "Hello World" should be seen.

12) All the latest changes should be pushed to git before the project goes to heroku. 

** Push all modified code changes to git.

Final Code Structure :

Create App in Heroku

13) Login to heroku with your free account that you have created in the first place.

14) Create a new app from heroku dashboard by clicking [New -> Create new app]

15) Give your application name and click "Create"

16) Check the GIT Url of the application from the settings tab.


17) Go to the terminal of your nodejs project. Execute the following command.

 $ git remote add fstheroku

18) Now push the code to heroku

$ git push fstheroku master

19) Open the deployed url in the browser. In my case it is

No comments:

Post a Comment