MongoError: getaddrinfo ENOTFOUND undefined undefined:27017

txnnxr picture txnnxr · Aug 23, 2016 · Viewed 35.2k times · Source

So I'm working my way though the Getting Mean book from Manning and following the steps in Chapter 5 I'm trying to use a db on Mongolab as an add-on to Heroku. When I run this code (both locally and on Heroku) it returns this error:

MongoError: getaddrinfo ENOTFOUND undefined undefined:27017

This is my current code:

var mongoose = require('mongoose');

var dbURI = "mongodb://localhost/loc8r";
if (process.env.NODE_ENV === 'production') {
    dbURI = process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);

Troubleshooting I started the app from the terminal with:

NODE_ENV=production MONGOLAB_URI=/*my mongo uri here*/

with that it runs fine locally. So I'm not sure where the issue is coming from. Any suggestions for troubleshooting the error listed?

Answer

Ravi Shankar Bharti picture Ravi Shankar Bharti · Aug 23, 2016

I think you are not providing the PORT NO. required for mongoDB.

Please give the port no.(27017) along with localhost.

Try this:

var dbURI = "mongodb://127.0.0.1:27017/loc8r";

getaddrinfo ENOTFOUND means client was not able to connect to the given address. Please try with the above address.

I hope this helps.