MongoClient.connect not working from nodejs

I am a beginner in mongodb, trying to establish a connection to mongodb from nodejs. There is no error and it just hangs in the terminal window in VS code.

I set up the environment variable Path to C:\Program Files\MongoDB\Server\6.0\bin
and ran ‘npm install mongodb’.
“mongodb://localhost:27017/” is running in my local docker desktop.
“mongodb://localhost:27017/” succcessfully connects from Mongodb for VS code tool but not connecting from VS code nodejs. Below is my code and any help would be greatly appreciated!!

const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
//const url = "mongodb://";

MongoClient.connect(url, { useUnifiedTopology: true }, function (err, client) {
  if (err) {
  } else {
    console.log("Database Connected");


Did you try with the line which is commented in your code?

Thanks for your response. Yes I did try but not connecting. I am seeing below errors in the mongod log files.

“msg”:“Failed to check socket connectivity”,“attr”:{“error”:{“code”:6,“codeName”:“HostUnreachable”,“errmsg”:“Connection reset by peer”}}}
“msg”:“Interrupted operation as its client disconnected”,