How to deploy a Webapp to Heroku (Node.js, React.js and MongoDB)
Tags: heroku, node, react, mongodb
Published: 2019-07-05 03:18:19

---
title: How to deploy a Webapp to Heroku (Node.js, React.js and MongoDB)
published: true
description: A deployment of a webapp made with Node.js, React.js and MongoDB to Heroku
tags: Heroku, Node.js, React.js, MongoDB
---

On my [last post](https://dev.to/thisk8brd/my-fullstack-setup-node-js-react-js-and-mongodb-2a4k) I explained how to create a full-stack webapp with Node.js, React.js and MongoDB.
Now I am going to show how to deploy this app to Heroku.

If you don't have it yet, you can find the source code [here](https://github.com/thisk8brd/node-react-starter).
*I recommend cloning this repo to follow the tutorial, but you are free to try the tips below on your on project.*

After cloning the project, you have to create an account at [Heroku](https://www.heroku.com)

> Heroku is a cloud server that makes it really easy to deploy applications with an easy and simple way, using git commands. This server also has very nice free plans where you can try many types of applications, databases and much more.

After creating and signing in to your Heroku account, you can create a heroku app. We can do that in the command line.
Navigate to the root of the project you just cloned and run the following commands:

```
$ heroku login
$ heroku create <name of your app>
```

You should get the following response:

![](https://thepracticaldev.s3.amazonaws.com/i/ofjzu66zm8ajcdlly094.png)

Now, in the package.json in the root of the project, add the following line under scripts:

```json
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"     
```

Your package.json file should look like this:

```json
{
  "name": "node-react-starter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "client": "npm run start --prefix client",
    "server": "nodemon index.js",
    "dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
    "start": "node index.js",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"    
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongoose": "^5.6.3"
  },
  "devDependencies": {
    "concurrently": "^4.1.1",
    "nodemon": "^1.19.1"
  }
}

```

Now, save your file and commit the changes you made.

```
$ git commit -am "Added heroku postbuild scripts"
```

As we have already created a Heroku app, we can just push it to the server.

```
$ git push heroku master
```

You should see some build scripts running and a message at the end saying that your app was deployed to Heroku successfully.

Now, you can navigate to your [Heroku Dashboard](https://dashboard.heroku.com/apps) and see that your app was deployed successfully.

But this app is not ready yet, we still need to add the database.

![](https://media.giphy.com/media/cMVgEhDeKzPwI/giphy.gif)

Click to enter in your project on Heroku. You should see something like this:

![](https://thepracticaldev.s3.amazonaws.com/i/xx98adpj02cdquacwv75.png)

- Click in Configure Add-ons.
- Search for mLab MongoDB.
- Click it and select the option Sandbox - Free

*If you navigate to Settings and click in the option Reveal vars, you will see that it added automatically the MongoDB environment variable.*


Now, your project should be up and running and you can see that by clicking in the button Open app, in the top of the Heroku dashboard page.

And Voila! We have a production ready full-stack project successfully deployed.

![](https://media.giphy.com/media/CiTLZWskt7Fu/giphy.gif)

Hope you find this tutorial useful.