Cannot connect to MongoDB because of wrong URI
Asked Answered
E

6

25

I was trying to run mongoDB on node server Full Code here from MongoDB:

My mongo version: 4.4.3

Node version: v15.7.0

I've imported get started code from MongoDB, and here's the code:

const { MongoClient } = require("mongodb");
// Connection URI
const uri =
  "mongodb+srv://sample-hostname:27017/?poolSize=20&writeConcern=majority";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
  try {
    // Connect the client to the server
    await client.connect();
    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

On terminal, when i run "node app.js", it throws me following error:

> (node:79653) Warning: Accessing non-existent property 'MongoError' of
> module exports inside circular dependency (Use `node --trace-warnings
> ...` to show where the warning was created) MongoParseError: URI does
> not have hostname, domain name and tld
>     at parseSrvConnectionString (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:50:21)
>     at parseConnectionString (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:594:12)
>     at connect (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/operations/connect.js:284:3)
>     at /home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:225:5
>     at maybePromise (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:681:3)
>     at MongoClient.connect (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:221:10)
>     at run (/home/harmony/Desktop/FruitsProject/app.js:12:18)
>     at Object.<anonymous> (/home/harmony/Desktop/FruitsProject/app.js:21:1)
Exercise answered 4/2, 2021 at 16:34 Comment(2)
Is there more code? The bottom of the stack suggests there's more code.Jocular
@Jocular Please take a look at docs.mongodb.com/drivers/node/fundamentals/connection for the full code from MongoDB site, Thank you!Exercise
E
40

The error Accessing non-existent property 'MongoError' of > module exports inside circular dependency is caused by a bug in mongodb 3.6.4

It's already reported here

Back to version 3.6.3 works for me:

npm uninstall mongodb --save

Install version 3.6.3

npm i [email protected]
Economizer answered 8/2, 2021 at 13:8 Comment(1)
To reference a comment in that thread from a MongoDB employee ".... I checked in with the Node driver team. The warning is safe to ignore and will hopefully be gone in an upcoming release." LinkCorticosteroid
Y
12

For everyone searching about this warning, don't worry, it's just a version bug, and was already reported. Just uninstall the 3.6.4 version and install the version 3.6.3 as answered on @kmgt answer.

More details:
https://developer.mongodb.com/community/forums/t/warning-accessing-non-existent-property-mongoerror-of-module-exports-inside-circular-dependency/15411

https://github.com/Automattic/mongoose/issues/9900

Yacano answered 8/2, 2021 at 15:31 Comment(2)
Why wouldn't you just ignore the warning for this mongoError thing?Willwilla
It's never a good idea to ignore warnings, if you research them, and see it's nothing important, then sure, you can ignore. But if something appears on your terminal saying some nonsense you're not sure of, I would suggest to at least search for a little while about it.Yacano
W
9

I downgraded MongoDB as recommended but that alone didn’t solve the problem.

I had to downgrade mongoose too to get the error removed.

I downgraded to:

MongoDB version 3.6.3 Mongoose version 5.11.15

Waziristan answered 15/2, 2021 at 0:2 Comment(0)
S
1

You don't have to downgrade your MongoDB, just downgrade your mongoose package to 5.11.15 as in this mention: https://github.com/Automattic/mongoose/issues/9900#issuecomment-778535254 I'm using Mongo v4.4.3 and just downgrade mongoose worked for me.

Spumescent answered 18/2, 2021 at 17:18 Comment(0)
C
0

I have a similar error too, am running on MacOS Catelina:

(node:3265) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency (Use `node --trace-warnings ...` to show where the warning was created) Server started on port 3000

Previously running on:

node: v14.15.4, mongodb: v4.4.3, mongoose: v5.11.18

After downgrading mongoose to v5.11.15, the error is gone.

  1. Uninstall mongoose from terminal
    npm uninstall mongoose

  2. Install mongoose version 5.11.15
    npm i [email protected]

Chatwin answered 27/2, 2021 at 10:47 Comment(0)
R
0

[mongo error 1

because of the line which I show in red rectangler over the picture

Replenish answered 3/3, 2021 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.