Heroku is a platform as a service (PAAS) which supports deployment of applications written in various programming languages such as Node.js, Ruby, Java, PHP, Python, Go, Scala, and Clojure. Heroku provides containers for running applications and packages application’s code and dependency into containers. Containers are lightweight isolated environments that provide compute, memory, OS and ephemeral filesystem.

In this tutorial we will show you how to deploy a simple Node.js application on Heroku.

Installing Heroku Command Line Interface (CLI)

We will use Heroku Command Line Interface (CLI) to manage and scale our applications, to provision add-ons, to view the logs of our application, as well as to run our application locally. Next you should download and install Heroku Command Line Interface for your system. Install Heroku CLI with default options selected.

Also make sure you have node, npm, and git installed. Next login into Heroku using CLI.

$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password:
...

Preparing the Node.js app

Next, we will prepare a sample Node.js application for deploying on Heroku. In this tutorial, we will clone and run sample application provided by Heroku. Execute following commands in your command shell or terminal for cloning the application and navigating into code directory:

$ git clone https://github.com/heroku/node-js-getting-started.git
$ cd node-js-getting-started

You now have simple Node.js application in a git repository. package.json contains application dependencies and is used by Node’s dependency manager.

Deploying the app

In this step we will deploy our application on Heroku.

Create an app on Heroku, which will prepare Heroku to receive application’s code:

$ heroku create
Creating app... done, ⬢ quiet-inlet-21223
https://quiet-inlet-21223.herokuapp.com/ | https://git.heroku.com/quiet-inlet-21223.git

When we create Heroku app, a git remote is created and is associated with our local repository. Heroku also generates random name for our app. We can also specify application name while creating the app, if we don’t want random name.

Now for deploying the code, execute following commands:

$ git push heroku master
Counting objects: 488, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (366/366), done.
Writing objects: 100% (488/488), 231.26 KiB | 14.45 MiB/s, done.
Total 488 (delta 87), reused 488 (delta 87)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  8.9.1
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 8.9.1...
remote:        Downloading and installing node 8.9.1...
remote:        Using default npm version: 5.5.1
remote:
remote: -----> Restoring cache
remote:        Skipping cache restore (not-found)
remote:
remote: -----> Building dependencies
remote:        Installing node modules (package.json)
remote:        added 132 packages in 3.73s
remote:
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (nothing to cache)
remote:
remote: -----> Pruning devDependencies
remote:        Skipping because npm 5.5.1 sometimes fails when running 'npm prune' due to a known issue
remote:        https://github.com/npm/npm/issues/19356
remote:
remote:        You can silence this warning by updating to at least npm 5.7.1 in your package.json
remote:        https://devcenter.heroku.com/articles/nodejs-support#specifying-an-npm-version
remote:
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 19M
remote: -----> Launching...
remote:        Released v3
remote:        https://quiet-inlet-21223.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/quiet-inlet-21223.git
 * [new branch]      master -> master

Our Node.js application is now deployed on Heroku. Now we have to make sure that atleast one instance of our application is up and running. We can do this using following command:

$ heroku ps:scale web=1

Congratulations, you now have an simple Node.js application running on Heroku. Now visit the app at the url generated by its app name.

$ heroku open

 

Here are some handpicked articles you might find useful:


Leave a Reply

%d bloggers like this: