Search code examples
node.jsmongodbmongoosemacos-big-sur

Mongoose NodeJS macOS Big Sur MongooseServerSelectionError: connect ECONNREFUSED ::1:27017


I'm using brew mongodb-community on macOS Big Sur and can connect to mongo shell with mongosh. But when I try to run NodeJS and Mongoose app I get MongooseServerSelectionError: connect ECONNREFUSED ::1:27017. Why can I connect to mongodb with mongo shell but not NodeJS and Mongoose?

Edit: Sample code from my NodeJS Mongoose code

import mongoose from 'mongoose';
mongoose.connect('mongodb://localhost:27017/mydb', {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

Solution

  • My solution was to use 127.0.0.1 and NOT localhost. For some reason Mongoose can read mongodb://127.0.0.1:27017/mydb and NOT mongodb://localhost:27017/mydb