Skip to content Skip to sidebar Skip to footer

MongooseError: The `uri` Parameter To `openUri()` Must Be A String

I have a problem with my code I used this code in app.js: const mongoose = require('mongoose'); const dotenv = require('dotenv'); dotenv.config() mongoose.connect(process.env

Solution 1:

This also occurs when you aren't saving your .env file in the root folder.


Solution 2:

Save your env file as .env not env.txt and make sure it's in your root folder as well & see if that works! Also MONGO_URI is the traditional naming convention but that shouldn't matter.

mongoose
    .connect(process.env.MONGO_URL, {
        useNewUrlParser: true,
        useCreateIndex: true,
        useUnifiedTopology: true
    })
    .then(() => {
        console.log('Connected to Mongo!');
    })
    .catch((err) => {
        console.error('Error connecting to Mongo', err);
    });

Additionally you can simplify your 2 require statements above with the lines below and you should call this as soon as possible:

require('dotenv').config();
const mongoose = require('mongoose')

Solution 3:

Require dotenv like this. It worked for me

require('dotenv').config();

Solution 4:

first of all, the env file should be saved as .env not env.txt, .txt is an extension for text files.

Also, I don't think you have named your project 'test' so make sure to change it to your current project name.

If you still get an error, do not use environment variables instead add the whole mongodb path inside mongoose.connect() in double or single quotes.

mongoose.connect(process.env.mongodb, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useCreateIndex: true,
});

Solution 5:

I too was getting the same errors, It was crashing due to the environment variable MONGODB_URI not being set and running heroku config returned with no variables in the cli.

The fix was running : heroku addons:create mongolab:sandbox

If heroku push origin master is needed, then heroku config should now show your .env variable.


Post a Comment for "MongooseError: The `uri` Parameter To `openUri()` Must Be A String"