MongoError: connect ECONNREFUSED 127.0.0.1:27017

My apps stopped working after upgrading from Nodejs 14 to 17.

The error I got was

MongoServerSelectionError: connect ECONNREFUSED ::1:27017

The solution was simply replacing localhost by 0.0.0.0. I.e. in my source code I had to change

const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);

to

const uri = "mongodb://0.0.0.0:27017/";
const client = new MongoClient(uri);

I am posting this as an answer because I couldn’t find the solution anywhere on the Internet and I wasted a lot of time on this one…

Leave a Comment