Error: getaddrinfo ENOTFOUND when making HTTP request [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
5 min

banner

# Table of Contents

  1. Error: getaddrinfo ENOTFOUND when making HTTP request
  2. Don't specify the protocol in the host property
  3. Make sure the server is not down or inaccessible
  4. Make sure the environment variables in your URL are resolved correctly
  5. Make sure the protocol is correct
  6. Make sure your /etc/hosts file is configured correctly
  7. Solving the error when trying to connect to MongoDB
  8. If you use a proxy, remove it or configure it correctly

# Error: getaddrinfo ENOTFOUND when making HTTP request [Fix]

The Error "getaddrinfo ENOTFOUND" when making an HTTP request occurs for multiple reasons.

Make sure you haven't specified an incorrect URL when making the request (e.g. one that contains spaces or is misspelled).

shell
# ⛔️ Incorrect (contains a space) https://bobby hadz.com # ✅ Correct https://bobbyhadz.com

Make sure the complete URL to which you're making the request hasn't been misspelled and does not contain special characters.

# Don't specify the protocol in the host property

Don't specify the protocol (e.g. https or http) when setting the host property in the options object.

If you set the host property in an options object, make sure the protocol scheme (https:// or http:// is not set).

The following is correct.

index.js
// ✅ Correct (not specifying protocol scheme in host) const options = { host: 'bobbyhadz.com', path: '/books' }

The following is incorrect.

index.js
// ⛔️ Incorrect (specified protocol scheme in host) const options = { host: 'https://bobbyhadz.com', path: '/books' }

You can also set the port if necessary.

index.js
// ✅ Correct (not specifying protocol scheme in host) const options = { host: 'bobbyhadz.com', port: 5000, // 👈️ optional, defaults to port 80 path: '/books', }

The http.get and https.get methods (and many other HTTP clients) can be called as follows.

index.js
// ✅ (1) with a complete URL http.get('https://bobyhadz.com/books', callback) // ✅ (2) or with an options object const options = { host: 'bobbyhadz.com', path: '/books' } http.get(options, callback)

In other words, the syntax is http.get(URL, callback) or http.get(options, callback).

If you set the host property in any HTTP client, make sure the protocol scheme (http:// or https://) is NOT specified.

Notice that the path (e.g. /books) is also not included in the host property.

You can also specify query parameters when setting the path.

index.js
// ✅ correct (not specifying protocol scheme in host) const options = { host: 'bobbyhadz.com', path: '/books?page=2&limit=10', // post: 3000, // 👈️ if you need to specify the port }

If you're making requests in Node.js with the http or https modules, make sure to use https when the protocol is secure.

index.js
// for secure SSL requests (https) import https from 'https'; // or for insecure (http) requests import http from 'http';

# Make sure the server is not down or inaccessible

  • The server to which you're making a request might be down or not accessible (e.g. accessible only internally).

You can try to ping the server to make sure it's online and accessible.

shell
ping -c 3 google.com

If you are on macOS, try to restart your DNS service by issuing the following command.

shell
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

If you use a proxy, make sure your proxy settings are correct.

# Make sure the environment variables in your URL are resolved correctly

You might be making an HTTP request where the URL contains environment variables that are not resolved to the correct values (e.g. in Postman).

The error commonly occurs when you have environment variables that have not been resolved correctly when using Postman.

For example, in Postman when you hover over a variable that hasn't been resolved, you get the message "Unresolved Variable".

postman unresolved variable

  1. Make sure the environment to which you added the environment variable is active.

  2. Make sure the variable is set correctly and gets resolved.

  3. View the value of the environment variable in the Console tab after issuing the request.

# Make sure the protocol is correct

Make sure the protocol is correct (e.g. https:// for secure requests and http:// for insecure requests).

shell
# 👇️ for a secure request ( with a valid SSL certificate ) https://bobbyhadz.com/books # 👇️ for an insecure request ( without an SSL certificate) http://localhost:5000/books

If you are making an HTTP request to an endpoint with a self-signed certificate:

  1. Try to remove the https:// protocol scheme.
  2. If that doesn't work, try to use the http:// protocol scheme instead.

# Make sure your /etc/hosts file is configured correctly

Make sure the /etc/hosts file on your machine has the following line.

/etc/hosts
127.0.0.1 localhost

You can edit your /etc/hosts file with your preferred text editor.

shell
# with Gedit sudo gedit /etc/hosts # or with Nano sudo nano /etc/hosts # or with VIM sudo vim /etc/hosts

Once you open your /etc/hosts file, make sure it has the following line.

/etc/hosts
127.0.0.1 localhost

The line is used to configure the loopback interface when the system is booting.

If you make changes to your /etc/hosts file, you might have to run the following command.

shell
sudo killall -HUP mDNSResponder

Or simply restart your PC for the changes to take effect.

# Solving the error when trying to connect to MongoDB

If you got the error when trying to connect to MongoDB:

  1. Try to change the URL parameter from localhost to 127.0.0.1 when calling mongodb.MongoClient.connect().
index.js
// ⛔️ Before MongoClient.connect( 'mongodb://localhost:27017/mydb', function (err, db) {} ) // -------------------------- // ✅ Try this MongoClient.connect( 'mongodb://localhost:27017/mydb', function (err, db) {} ) // -------------------------- // ✅ Or this MongoClient.connect( 'mongodb://127.0.0.1:27017/mydb', function (err, db) {} )
  1. The "getaddrinfo ENOTFOUND" error indicates that the hostname you specified when trying to connect to MongoDB cannot be resolved or is incorrect.

Make sure that your connection string is complete and correct.

Try to ping the hostname from your terminal to make sure the server is online and accessible.

shell
ping -c 3 YOUR_MONGO_HOSTNAME

If you just recently stood up a new MongoDB server, try to wait a few minutes as there might be a delay before the DNS information propagates.

# If you use a proxy, remove it or configure it correctly

You might also get the error when trying to NPM install a module from behind a proxy that is configured incorrectly.

If that is the case, you could remove the proxy setting.

shell
npm config rm proxy npm config rm http-proxy npm config rm https-proxy

I have written a detailed article on how to clear your proxy settings in NPM or npm install behind a proxy.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.