Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

516
Views
MONGO_URI in .env not working when deploying in heroku

I'm trying to deploy an app on heroku but i can't if i use dotenv. This code doesn't work:

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true }, () => {
  console.log("Connected to db successfully");
});

But this does:

mongoose.connect("mongodb+srv://test:test@cluster0.j0you.mongodb.net/ecommerce?retryWrites=true&w=majority", { useNewUrlParser: true }, () => {
  console.log("Connected to db successfully");
});

When i try to use dotenv heroku throws this error:

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I had to go to heroku dashboard> settings> config vars and then add the key as MONGO_URI and the mongo url value that mongodb gave me

about 4 years ago · Juan Pablo Isaza Report

0

You need to make sure of two things

  1. go to heroku dashboard> settings> config vars and then add the key as MONGO_URI and the mongo url value that mongodb gave me... this info is in the .env file of your project. you might have with another name like DATA_BASE_MONGO just make sure to use that name and key in the config vars.
  2. Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env, so you can pull it from there. Switch your listen to this: .listen(process.env.PORT || 5000), probably yo have it like .listen(5000), just make sure to change it. And thats`s it you will have your app running.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!