ECONNREFUSED error when connecting to mongodb from node.js

ECONNREFUSED error

There are few reasons of this error in node :

  1. Your port is already serving some service so it will refuse your connection.

    go to command line and get pid by using following command

    $ lsof -i:port_number

    Now kill pid by using

    $ kill -9 pid(which you will get by above command)

  2. Your server is not running e.g. in this case please check your mongoose server is running or run by using following command.

    $ mongod

  3. There is also possibility your localhost is not configured properly so use 127.0.0.1:27017 instead of localhost.


OK, this was another case of not being truly forthcoming in the info I posted above. My node.js app was very simple, but I was including another couple lines in my node.js code that apparently caused this issue.

Specifically, I had another variable declared which was calling some other code that made a separate database call using incorrect db info. This is why, when using Xinzz's code, the console log error seemed not to change. It wasn't actually the mongoose.connect command that was throwing the error!

Lesson learned, localize the problem and comment out unrelated code! Sorry guys, I knew this was me being dumb.