How to deploy your Node.js & MongoDB app/ REST-API to Heroku using Heroku CLI (.env variables included)

All the necessary commands use to deploy will be there in this article.

Prerequisites

  1. Node.js and npm installed.
  2. Node.js and MongoDB application.
  3. Free Heroku account.
  4. Heroku CLI.
  5. GitHub account.
  6. Git (Not necessary but it’s best to push code to github using git bash).

Important lines of code to check before moving ahead.

  1. Specify the version of the node in package.json file-:

(Find you node version by typing node --version in command prompt)

"engines": {
"node": "14.X"
}

2. specify the start command in the package.json file-: ex nodemon, nodemon server.js, npm start, etc.

"scripts": {
"start": "nodemon server.js"
}

(From 2013 procfile is not necessary for node app and it searches for above command to start the app).

3. Node version should be 10 or higher.

Configure Your .env variables

  1. Set the PORT in .env file to Heroku’s production port.(By default Heroku use port number 80).
  2. Set the DATABASE_CONNECTION_URL in .env file to connect with the database without exposing the credentials.

How to set and use .env variables

Note: Install dotenv npm package to use the .env file in project.

  • run yarn add dotenv or npm i dotenv
  • Make a file named .env in the root directory of the project folder and set variables as following-:
DATABASE_CONNECTION_URL= mongodb+srv://[User]:[password]@cluster0.pi2rv.mongodb.net/[Db name]?retryWrites=true&w=majorityPORT = 80 
  • import ‘dotenv/config’ at the top of the required file -:
require(‘dotenv/config’);// in server.js const port = process.env.PORT || 3000;mongoose.connect(process.env.DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true }, () => {console.log('connected to database')})

Push the code to Github If not done

$ git add .
$ git commit -m "Adding files to github"
$ git push origin main

Setup Heroku Project

If you haven’t made the Heroku free account yet make one.

Install the Heroku command line client. If you are not already logged in run command Heroku login , you will be redirected to your browser for login.

Steps to create project and push code (go to you project directory in git bash)-:

  • Create Heroku app and name it
$ heroku create your-unique-appname
  • Checked that if the app is created or not by running following command-:

(You can also verify on your heroku dashboard)

$ git remote -v// OUTPUT 
heroku https://git.heroku.com/your-unique-appname.git (fetch)
heroku https://git.heroku.com/your-unique-appname.git (push)
origin https://github.com/github-username/repo-name.git (fetch)
origin https://github.com/github-usernme/repo-name.git (push)()

Push code to Heroku

$ git push heroku main

Push .env variables to Heroku

$ heroku config:set var_name = var_value

or you can set the variable from your web dashboard go to https://dashboard.heroku.com/apps/your-appname/settings and provide config var over there.

Your app is on the web by now 😃 😙

Ensure it by running following commands-:

$ heroku open

Connecting Github repository

https://dashboard.heroku.com/apps/your-project-name/deploy/github

Go to the above link by replacing your project name and there will be an option to connect github. In very few steps you have connected you github repository.

After connecting github repository to heroku app you just have to push the code to github and you app will be automatically build and deployed.

App is not working after pushing the code ?? 😩 😫

Run the following command-:

$ heroku logs --tail

You can find all the error logs and then google it up to resolve.

If you can’t fix it after googling and searching on stack overflow, comment your error below and I will do my best to fix that error.

Umang is a developer, tech enthusiast, and a photographer. Loves to code with coffee on desk.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store