At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

How To Deploy NodeJS App to Heroku

  • By Ken
  • July 2, 2018
  • 942 Views

Today, I am going to demonstrate how to deploy a Node.js application on Heroku with a sample example, before getting into the steps let’s understand what is heroku.

What is Heroku?

Heroku is a cloud platform as a service supporting several programming languages that is used as a web application deployment model. Initially, it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go. For this reason, Heroku is said to be a polyglot platform. It lets the developer build, run and scale applications in a similar manner across all the languages.

Sample example to deploy a NodeJS app with heroku

Note: Heroku Node.js support will only be applied when the application has a package.json file in the root directory or application.

Prerequisites:

  • Node.js and npm installed.
  • Node.js application.
  • Heroku account.
  • Heroku CLI.

 

Create NodeJS app:

Following command will create the NodeJS app folder structure
Step 1: $ npm install -g express-generator
Step 2: express application_name
Once the above commands are done, the package.json file will available inside application_name

The generated package.json file looks like this:

 {
"name": "application-name",
"version": "0.0.0",
"private": true,
"scripts": {
  "start": "node ./bin/www"
},
"dependencies": {
  "body-parser": "~1.18.2",
  "cookie-parser": "~1.4.3",
  "debug": "~2.6.9",
  "express": "~4.15.5",
  "jade": "~1.11.0",
  "morgan": "~1.9.0",
  "serve-favicon": "~2.4.5"
}
}

 

Specify the version of node:

  1. The version of Node.js that will be used to run your application on Heroku, should also be defined in your package.json file. You should always specify a Node.js version that matches the runtime you’re developing and testing.
  2. If you not mention Node.js version, heroku will take default latest version and it will run your application on the node engine. But in any case we need to define the Node.js version is good choice.

 

package.json file will look like this:

 

{
"name": "application-name",
"version": "0.0.0",
"private": true,
"scripts": {
  "start": "node ./bin/www"
},
"dependencies": {
  "body-parser": "~1.18.2",
  "cookie-parser": "~1.4.3",
  "debug": "~2.6.9",
  "express": "~4.15.5",
  "jade": "~1.11.0",
  "morgan": "~1.9.0",
  "serve-favicon": "~2.4.5"
},
"engines": {
      "node": "8.11.1"
  }
}

 

Build your app and run it locally:

Step 1 : Run the npm install command in your local app directory to install the dependencies that you declared in your package.json file.

$ cd application_name && npm install

 
Step 2 : Start your app locally using the heroku local command, which is installed as part of the Heroku CLI.

     $ heroku local web

 
Step 3: App should now be running on

http://localhost:3000/

 
 

Deploy your application to Heroku:

After you commit your changes to git, you can deploy your app to Heroku
Step 1:

$ git init

 

Initialized empty Git repository in /home/ken/Desktop/Nodejs/application_name/.git/

Step 2:

$ git add .

 
Step 3:

$ git commit -m "Added a sample app"
[master (root-commit) 31a87b2] Added a sample app
9 files changed, 197 insertions(+)
create mode 100644 app.js
create mode 100755 bin/www
create mode 100644 package.json
create mode 100644 public/stylesheets/style.css
create mode 100644 routes/index.js
create mode 100644 routes/users.js
create mode 100644 views/error.jade
create mode 100644 views/index.jade
create mode 100644 views/layout.jade

 
Step 4:

$ heroku login
Enter your Heroku credentials.
...

 
Step 5:

$ heroku create
▸    heroku-cli: update available from 6.15.19-f4f84f6 to 6.16.18-62346b1
Creating app... done, ⬢ intense-temple-92995
https://intense-temple-92995.herokuapp.com/ | https://git.heroku.com/intense-temple-92995.git

 
Step 6:

$ git push heroku master
...
-----> Node.js app detected
...
-----> Launching...
Released v3
https://intense-temple-92995.herokuapp.com/ deployed to Heroku
To open the app in your browser, type $ heroku open.
https://intense-temple-92995.herokuapp.com/

 
Unlike other service providers, Heroku will reduce the manual configuration and package installations. With help of Heroku, we can completely get the required installed files and configurations and With just few steps we can easily run the application just by mentioning the Root directory, dependency files and the version of app.