MongoDb Atlas not connecting to Heroku MongoDb Atlas not connecting to Heroku heroku heroku

MongoDb Atlas not connecting to Heroku


By default process.env.NODE_ENV is production on Heroku, so it won't use .env file even if it is under the project folder.

Adding MONGO_URI and your mongodb connection string key value pair in Heroku should do the trick:

  1. login Heroku
  2. select your app
  3. click on settings tab
  4. find 'Config Vars' section
  5. click 'Reveal Config Vars'
  6. add MONGO_URI as KEY and your connection string(no quote) as VALUE
  7. click on add button
  8. open your app again

Update:I created a demo using your above code and deployed on Heroku following above steps after connecting it to my git repo. It works fine.

Live demo

Source code

You can also check the node console by clicking More -> View logs in the Heroku app page(top right).