loading...
Cover image for How To Quickly Deploy Node Js App To Heroku

How To Quickly Deploy Node Js App To Heroku

3sanket3 profile image Sanket Patel Originally published at 3sanket3.com ・3 min read

This tutorial will cover how to deploy the Node.js project to Heroku - the cloud application platform. We will have our application up by running few commands.

Assuming you have any node.js app that you want to publish. If not, I have listed the steps in this tutorial. The codesandbox for the same is here to set up the basic app.

Prerequisite

  • Git

    • To use the Huroku CLI we will need to have the Git installed in the machine, so make sure you have it.

You can check if it is already installed or not using

    $ git --version
    git version 2.18.0.windows.1
  • Node 8+
  $ node --version
  v8.12.0
  • A registered account at https://heroku.com
    • Please sign up and verify your email address, if you haven't.

Initialize the Git for the project (if it isn't)

If your project is the git repository itself then move to install heroku

$ git init
Initialized empty Git repository in your-project-path/.git/

Create a .gitignore file to ignore node_modules

$ echo node_modules > .gitignore

Commit the files to git repository

$ git add -A

$ git commit -m 'initial commit'
[master (root-commit) e79168d] initial commit
 4 files changed, 2047 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 index.js
 create mode 100644 package.json
 create mode 100644 yarn.lock

Install Heroku

Download and install the Heroku CLI as per your platform.

Check if it is installed by checking version.

  $ heroku --version
  heroku/7.24.3 win32-x64 node-v11.14.0

Login to Heroku

heroku login command will ask us to open a browser using pressing any key. Then you can login in browser and the terminal will catch it. Isn't it cool!

$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/06c81181-c988-457f-b415-5789e7abd758
Logging in... done
Logged in as 3sanket3@gmail.com

Create a Heroku app

Create the app using command heroku create <optional-app-name>

$ heroku create nodejs-tutorial-one-3sanket3

Creating ⬢ nodejs-tutorial-one-3sanket3... done
https://nodejs-tutorial-one-3sanket3.herokuapp.com/ | https://git.heroku.com/nodejs-tutorial-one-3sanket3.git

Make sure the package.json of your project have a start command

...
"scripts": {
    "dev": "nodemon index.js", <-- for local development purpose
    "start": "node .", <-- Heroku will use to start the application
    "test": "echo \"Error: no test specified\" && exit 1"
  },
...

Note: If you want to use start command for some other task, you can create a Procfile . It will tell Heroku which command to run while starting the application, as explained here.

Deploy the code

$ git push heroku master

If everything goes fine you should see the last part of the command output as below. It contains the URL at which your app is deployed.

heroku deployment output

Check deployment logs

Using heroku logs --tail command, you can check logs of deployment. It will be updated realtime so you can keep it open while deployment, in another terminal tab/window keep your eye on it.

Ref : https://devcenter.heroku.com/articles/getting-started-with-nodejs
Cover Photo by Rakicevic Nenad from Pexels

Discussion

pic
Editor guide
Collapse
rohovdmytro profile image
Rohov Dmytro

Cool!

This is what I was doing before migrating to use dokku and Digital Ocean.

Collapse
3sanket3 profile image
Sanket Patel Author

I would love to know the reason for the migration, if you can share.

Collapse
rohovdmytro profile image
Rohov Dmytro

There was a reason definitely but I am not sure.

I might think that I need to run couple of simple websites and it was much cheaper to di via dokku with no downsides for my usecase.

Collapse
hammadsiddiqui profile image
Hammad Siddiqui

An addition of mLab add-on is always helpful since most node apps may require a DB