Last updated: Apr 5, 2024
Reading time·5 min
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).
# ⛔️ 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.
host
propertyDon'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.
// ✅ Correct (not specifying protocol scheme in host) const options = { host: 'bobbyhadz.com', path: '/books' }
The following is incorrect.
// ⛔️ Incorrect (specified protocol scheme in host) const options = { host: 'https://bobbyhadz.com', path: '/books' }
You can also set the port
if necessary.
// ✅ 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.
// ✅ (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
.
// ✅ 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.
// for secure SSL requests (https) import https from 'https'; // or for insecure (http) requests import http from 'http';
You can try to ping the server to make sure it's online and accessible.
ping -c 3 google.com
If you are on macOS, try to restart your DNS service by issuing the following command.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
If you use a proxy, make sure your proxy settings are correct.
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".
Make sure the environment to which you added the environment variable is active.
Make sure the variable is set correctly and gets resolved.
View the value of the environment variable in the Console tab after issuing the request.
Make sure the protocol is correct (e.g. https://
for secure requests and
http://
for insecure requests).
# 👇️ 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:
https://
protocol scheme.http://
protocol scheme instead./etc/hosts
file is configured correctlyMake sure the /etc/hosts
file on your machine has the following line.
127.0.0.1 localhost
You can edit your /etc/hosts
file with your preferred text editor.
# 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.
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.
sudo killall -HUP mDNSResponder
Or simply restart your PC for the changes to take effect.
If you got the error when trying to connect to MongoDB:
localhost
to 127.0.0.1
when calling
mongodb.MongoClient.connect()
.// ⛔️ 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) {} )
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: