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)
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 mongodb@3.6.3
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.
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
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.
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.
Uninstall mongoose from terminal
npm uninstall mongoose
Install mongoose version 5.11.15
npm i mongoose@5.11.15