---
title: "Discussion on \"Deploying MERN to Heroku (success)\" | Hashnode"
description: "Discussion on \"Deploying MERN to Heroku (success)\". Note for 2020: MERN stack is going out of style, as this was originally written in 2016 😱\n\nIf you haven't checked out the MERN project, do yourself a favor and check it now and enjoy some next level javascript architecture and tooling!\nThough instal..."
url: "https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Deploying MERN to Heroku (success)](https://hashnode.com/post/deploying-mern-to-heroku-success?utm_source=hashnode&utm_medium=feed)

Note for 2020: MERN stack is going out of style, as this was originally written in 2016 😱 If you haven't checked out the MERN project, do yourself a favor and check it now and enjoy some next level javascript architecture and tooling! Though instal...

May 14, 2016 · 3 min read · hashnode.com  
Updated Nov 19, 2020  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#mern](https://hashnode.com/tag/mern)
-   [#universal](https://hashnode.com/tag/universal)
-   [#heroku](https://hashnode.com/tag/heroku)
-   [#isomorphic](https://hashnode.com/tag/isomorphic)

## Written by

[Jake Foraker](https://hashnode.com/@jakeforaker)

@jakeforaker

  
[View profile](https://hashnode.com/@jakeforaker)

## Responses 7

Markdown supported · ⌘↵ to post

[sardar usama](https://hashnode.com/@SardarUsama)

[Nov 12, 2018](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/5be9d292691392e46ef3b262)

Getting the error of can, not post /adminLogin, unable to get data from the database (MongoDB) can anyone tell me? thanks

[Bedig Galladian](https://hashnode.com/@bgalladian)

[Jun 19, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/59485483fabd529b73565bd5)

Hi,

Great tutorial, I've successfully deployed my app, but there is no CRUD functionality. Can you provide any insight as to why? There seems to be little consensus as to why that is, and with fewer solution. Any help would be appreciated. Thanks!

kicksstarter.herokuapp.com

[Bedig Galladian](https://hashnode.com/@bgalladian)

[Jun 20, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/59485483fabd529b73565bd5#reply-59487d31fabd529b73565bf0)

Oh crap missed one, which mLablink should I put in there?

[Jake Foraker](https://hashnode.com/@jakeforaker)

[Jun 23, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/59485483fabd529b73565bd5#reply-594c822c5f4dd6ff593b21f7)

You shouldn't connect to your database via a GET request. It is bad practice and not recommended:

[docs.mlab.com/data-api](http://docs.mlab.com/data-api/#overview)

"The first method - the one we **strongly** recommend - is to connect using one of the [available MongoDB drivers](http://docs.mlab.com/connecting/#drivers). You do not need to use our API if you use the driver. In fact, using a driver provides better performance, better security, and more functionality.

The second method, documented in this article, is to connect via mLab’s RESTful Data API. **Use this method only if you cannot connect using a MongoDB driver**."

Your api is presumably in your heroku app, so you just set is as a relative path to the root. This is handled in server.js and you shouldn't have to change it: [github.com/Hashnode/mern-starter/blob/master/serv…](https://github.com/Hashnode/mern-starter/blob/master/server/server.js#L58)

You might to modify the start script in package.json and set the env to production like NODE\_ENV=production. See step 5 in the tutorial above.

[Bedig Galladian](https://hashnode.com/@bgalladian)

[Jun 23, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/59485483fabd529b73565bd5#reply-594d384de885a547a39911e5)

I'm going to try this when I get a chance, thank you so much!

[Fiona Nataprawira](https://hashnode.com/@nfiona)

[Apr 17, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/58f521cb07c49a70fb299500)

Hello!

I deployed my MERN app to Heroku, it is rendering properly and my third-party API search function works well. However, it seems like my GET and POST methods to add new data is not working now. I also lost the datas that were originally stored on my database. I got the below errors:

`GET [localhost/api/projects](https://localhost:3001/api/projects) net::ERR_CONNECTION_REFUSED     OPTIONS [localhost/api/projects](https://localhost:3001/api/projects) net::ERR_CONNECTION_REFUSED     Uncaught (in promise) Error: Network Error       at createError (createError.js:15)       at XMLHttpRequest.handleError (xhr.js:87)`

Originally I had the Mixed-Content error and I fixed that by adding the below to my index.html's head section: `<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">`

Here is link to my app: [https://fierce-sierra-67905.herokuapp.com/](https://fierce-sierra-67905.herokuapp.com/)

This is my first time deploying MERN app on Heroku and have struggled for a while. Any help would be much appreciated!

[Jake Foraker](https://hashnode.com/@jakeforaker)

[Apr 17, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/58f521cb07c49a70fb299500#reply-58f53a0a977c5336c6f48f29)

Looks like you forgot to update your own API url from your local development version. Make sure to change it to your new heroku URL. (Or omit the location and use just "/api/projects" if that makes sense for your configuration)

[Fiona Nataprawira](https://hashnode.com/@nfiona)

[Apr 17, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/58f521cb07c49a70fb299500#reply-58f540c1977c5336c6f48f2d)

Thank you [Jake Foraker](https://hashnode.com/@jakeforaker "'s Profile - Hashnode") ! That took care of the GET error, however my original datas are still not showing and I am still getting this:

```
POST fierce-sierra-67905.herokuapp.com 404 (Not Found)
```

My connection.js:

```
var mongoose = require("mongoose")
mongoose.connect("mongodb://localhost/projectbook", (err) => {
   if(err) {     
    console.log(err)  
 } else {     
console.log("MongoDB is Connected!")   } })

module.exports = mongoose
```

Package.json:

```
...

"scripts": {     
"start": "cross-env NODE_ENV=production MONGO_URL=mongodb://localhost/projectbook node index.js",     
"start-dev": "nf start -p 3000",     
"build": "react-scripts build",     
"test": "react-scripts/test.js --env=jsdom",     
"eject": "react-scripts eject"   },  
... 
"testEnvironment": "node",     
"testURL": "localhost", 
...
```

Am I supposed to deploy my mongodb on a separate heroku deployment?

Please pardon my ignorance, Thanks again for your help!

[Jake Foraker](https://hashnode.com/@jakeforaker)

[Apr 18, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/58f521cb07c49a70fb299500#reply-58f56de327471d3cc62907d5)

So it looks like you are trying to point to `localhost` again. You need a hosted instance of your database, with a connection string (url) that is accessible to the outside world (the interwebs). Luckily, Heroku makes this extremely easy with the mLab addon [elements.heroku.com/addons/mongolab.](https://elements.heroku.com/addons/mongolab.) All you have to do is add it, and you get a free tier instance.

You will replace your current `MONGO_URL` env variable with the new one provided by Heroku.

[Nigel Fish](https://hashnode.com/@nachopescado)

[Feb 20, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/58ab247f94d13a746b6cefa2)

So there's, in my view, a better way to do this. The issue is by removing dist from the gitignore, every time you're committing to your main repo, you're committing all these changed dist files which is a bit annoying. Also when you're pushing to Heroku, it's pushing in all the non-built files as well, and other things it doesn't need.

What you can do instead is create a new git in the dist directory and push that to heroku. This won't work out of the gate, what you need to do is:

-   move the built files into a sub-directory called dist inside dist (yes, /dist/dist).
-   copy package.json and index.js to the original dist (/dist)
-   initialize a new git inside /dist, add files, add heroku remote, and push to heroku.

Now you're not tracking every change to a distribution file in your main git repo. You could script the above to make the process even easier.

[Jake Foraker](https://hashnode.com/@jakeforaker)

[Feb 20, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/58ab247f94d13a746b6cefa2#reply-58ab3bfdad72c340923a341a)

Nice. This post was by no means a comprehensive best practice guide, more like how to get it live quickly. Thanks for sharing.

[Rohan Bagchi](https://hashnode.com/@rbagchi)

[Feb 7, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/589a0e7a93a28f88162ce19d)

I get the error `Error: Cannot find module './dist/server.bundle.js'`. I checked the dist folder and no `server.bundle.js` was generated. Any idea what could be going wrong?

[Rohan Bagchi](https://hashnode.com/@rbagchi)

[Feb 7, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/589a0e7a93a28f88162ce19d#reply-589a134ab8c198c4787ea4f3)

As it turns out, we have to run `npm run build:server`to generate the missing `server.bundle` file.

[Rohan Bagchi](https://hashnode.com/@rbagchi)

[Feb 8, 2017](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/589a0e7a93a28f88162ce19d#reply-589aa89a3fb380655d72c58d)

Also, we need to update the `renderFullPage` function in `server.js`. The generated `js` & `css` is mapped by `manifest.json` as `"/main.css": "/app.<some_hash>.css"` and `"/main.js": "/main.<some_hash>.js"`. So, the style sheet reference in generated html should be updated to `<link rel='stylesheet' href='${assetsManifest['/main.css']}' />` and the `js` reference to be updated to `<script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/main.js'] : '/app.js'}'></script>`.

Then run `npm run build:server` and deploy.

[Nick deLannoy](https://hashnode.com/@cosmicbuffalo)

[Jan 11, 2018](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/589a0e7a93a28f88162ce19d#reply-5a56ffc11a909b63185698a0)

Aha! Was finally able to deploy successfully after including these missing asset tags!

[Russell Wells](https://hashnode.com/@russellwells)

[Sep 18, 2016](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/57de59fefc5715e76b269716)

when adding your mongo url be sure to remove the <<>> around your username and password

[Mayank Chandola](https://hashnode.com/@mayank)

[May 15, 2016](https://hashnode.com/posts/deploying-mern-to-heroku-success/5737b4e8911fe184eb43bbb3/comment/57381de1efdf90345d75b863)

Awesome work @jakeforaker ! Send a PR to [mern-starter](https://github.com/Hashnode/mern-starter) after adding a link to your story in readme.
