Node applications on App Service Linux and getaddrinfo ENOTFOUND
Sometimes when making requests to external dependencies with Node applications, you may encounter an error showing “getaddrinfo ENOTOUND”. This can happen for a few different reasons, which we’ll go through below.
NOTE: These scenarios may not cover all reasons why this may occur, but more of the most common ones.
What is getaddrinfo ENOTFOUND?
This is can be looked as two parts for better clarity:
ENOTFOUND:
ENOTFOUND is a DNS lookup failure thrown back by Node specifically — doc, meaning — this is something Node has created and is not a POSIX error — but, this can happen from the two real POSIX errors below.
This can either happen from EAI_NONAME (reference) which can ultimately occur in scenarios like a DNS sever being reachable, but no matching name was found to the IP being looked up, a DNS server timing out, a defined DNS server not able to reached, and others.
The other is EAI_NODATA (reference that can prompt a ENOTFOUND from Node. This can occur if the nodename has no data, which the nodename could be a name associated with the hostname of the IP address for the machine.
getaddrinfo:
getaddrinfo is a function to help resolve DNS within the C standard libray. Under the hood for Node HTTP clients, such as the Node native HTTP module — or other clients like Axios, use dns.lookup() (source, ref) by default — which ultimately ends up calling getaddrinfo — which is what is actually returning the either of two POSIX errors; and lastly in turn node will return ENOTFOUND for this.
Possible causes
Although some reasons where address above, we’ll list some more day-to-day reasons on why this may be seen.
The remote address doesn’t exist
One big reason why this may occur is the remote address doesn’t actually exist. Which would likely be due to the URI being looked up in code is possibly mistyped. Take the below example, which uses Axios to make an outbound call to “google”. However, we have a typo here — where the URI is actually “https://google .com”.
This will throw the below error, which most of the time will usually look like this:
As expected, correcting the typo will resolve this. Another scenario here is for sites that plainly may not exist anymore — such a permanent removal of an FQDN or similiar. In those cases, the below troubleshooting can be used.
DNS related issues
DNS related issues can manifest in a few ways, but this is another source of ENOTFOUND , and argubly the more-so direct reason for this — since getaddrinfo is inheritly DNS related, as explained above.
Improperly configured DNS servers (if using custom DNS, Azure DNS is the default on Azure App Serivce) — or domains, may contribute to this. If using a custom DNS server and wanting to rule this out, WEBSITE_DNS_SERVER could be set to a test value of 8.8.8.8.
If the client (the Node application making the call) runs fine, and the endpoint is validated to be correct — try to run nslookup , dig , or even tcpping to see if the remote host can be resolved. As of writing this, all 3 of these utilities are installed in the current Blessed Images.
If on Azure App Service Linux, and using a Node Blessed Image, SSH is enabled by default. But if using a custom Docker Image, you’ll have to enable SSH — for that, view this blog here. Use the above utilities while SSH’d into your container.
Firewall and networking
A firewall set on the remote end can also cause ENOTFOUND issues — or other network functionality that may block inbound traffic can additionally cause this behavior.
If this is deemed to the case, aside from the command line utilities above — a quick test to rule this out if to open up traffic to test connectivity.
SNAT port exhaustion
Node’s HTTP client doesn’t reuse connections by default — and in this scenario, if not properly configured, can experience SNAT port exhaustion. This can also be the case with database connections and not implementing pooling.
See this blog post here for ways to reuse connecitons.
Ultimately, a side effect of this is also getaddrinfo ENOTFOUND errors periodically seen in stderr logging when trying to call to external resources. When SNAT ports are effectively used up (since anything over 128 is “best effort” allocation), connections may start outright failing, or no ports for outbound connection use can be allocated.
getaddrinfo ENOTFOUND errors would likely on the intermittent side in this scenario. To validate if you’re experiencing this scenario, go to the Azure Portal and review the Diagnose and Solve blade -> SNAT Port Exhaustion detector.
15 Common Error Codes in Node.js and How to Fix Them
You will encounter various kinds of errors while developing Node.js applications, but most can be avoided or easily mitigated with the right coding practices. However, most of the information to fix these problems are currently scattered across various GitHub issues and forum posts which could lead to spending more time than necessary when seeking solutions.
Therefore, we've compiled this list of 15 common Node.js errors along with one or more strategies to follow to fix each one. While this is not a comprehensive list of all the errors you can encounter when developing Node.js applications, it should help you understand why some of these common errors occur and feasible solutions to avoid future recurrence.
Want to centralize and monitor your Node.js error logs?
Head over to Logtail and start ingesting your logs in 5 minutes.
1. ECONNRESET
ECONNRESET is a common exception that occurs when the TCP connection to another server is closed abruptly, usually before a response is received. It can be emitted when you attempt a request through a TCP connection that has already been closed or when the connection is closed before a response is received (perhaps in case of a timeout). This exception will usually look like the following depending on your version of Node.js:
If this exception occurs when making a request to another server, you should catch it and decide how to handle it. For example, you can retry the request immediately, or queue it for later. You can also investigate your timeout settings if you'd like to wait longer for the request to be completed.
On the other hand, if it is caused by a client deliberately closing an unfulfilled request to your server, then you don't need to do anything except end the connection ( res.end() ), and stop any operations performed in generating a response. You can detect if a client socket was destroyed through the following:
2. ENOTFOUND
The ENOTFOUND exception occurs in Node.js when a connection cannot be established to some host due to a DNS error. This usually occurs due to an incorrect host value, or when localhost is not mapped correctly to 127.0.0.1 . It can also occur when a domain goes down or no longer exists. Here's an example of how the error often appears in the Node.js console:
If you get this error in your Node.js application or while running a script, you can try the following strategies to fix it:
Check the domain name
First, ensure that you didn't make a typo while entering the domain name. You can also use a tool like DNS Checker to confirm that the domain is resolving successfully in your location or region.
Check the host value
If you're using http.request() or https.request() methods from the standard library, ensure that the host value in the options object contains only the domain name or IP address of the server. It shouldn't contain the protocol, port, or request path (use the protocol , port , and path properties for those values respectively).
Check your localhost mapping
If you're trying to connect to localhost , and the ENOTFOUND error is thrown, it may mean that the localhost is missing in your hosts file. On Linux and macOS, ensure that your /etc/hosts file contains the following entry:
You may need to flush your DNS cache afterward:
On Linux, clearing the DNS cache depends on the distribution and caching service in use. Therefore, do investigate the appropriate command to run on your system.
3. ETIMEDOUT
The ETIMEDOUT error is thrown by the Node.js runtime when a connection or HTTP request is not closed properly after some time. You might encounter this error from time to time if you configured a timeout on your outgoing HTTP requests. The general solution to this issue is to catch the error and repeat the request, preferably using an exponential backoff strategy so that a waiting period is added between subsequent retries until the request eventually succeeds, or the maximum amount of retries is reached. If you encounter this error frequently, try to investigate your request timeout settings and choose a more appropriate value for the endpoint if possible.
4. ECONNREFUSED
The ECONNREFUSED error is produced when a request is made to an endpoint but a connection could not be established because the specified address wasn't reachable. This is usually caused by an inactive target service. For example, the error below resulted from attempting to connect to http://localhost:8000 when no program is listening at that endpoint.
The fix for this problem is to ensure that the target service is active and accepting connections at the specified endpoint.
5. ERRADDRINUSE
This error is commonly encountered when starting or restarting a web server. It indicates that the server is attempting to listen for connections at a port that is already occupied by some other application.
The easiest fix for this error would be to configure your application to listen on a different port (preferably by updating an environmental variable). However, if you need that specific port that is in use, you can find out the process ID of the application using it through the command below:
Node.js getaddrinfo ENOTFOUND
When using Node.js to try and get the html content of the following web page:
I get the following error:
I did already look up this error on stackoverflow, and realized that this is because node.js cannot find the server from DNS (I think). However, I am not sure why this would be, as my code works perfectly on www.google.com .
Here is my code (practically copied and pasted from a very similar question, except with the host changed):
Here is the source where I copied and pasted from : How to make web service calls in Expressjs?
http.get returns «getaddrinfo ENOTFOUND» error #5436
I can definitely say it’s NOT a DNS or a PROXY issue.
What I testet so far:
node.js sample code:
- Version: v5.5.0
- Platform: Linux buildroot 4.1.15 armv6l (raspberrypi)
The text was updated successfully, but these errors were encountered:
Yes. url and host are just placeholders.
getaddrinfo is by definition a DNS issue. Does dig host or nslookup host work? Does dns.lookup(host, console.log) work? Can you post the domain?
dig is not available for buildroot. nslookup is working and gives me the ip address back. I tested lots of different domains (ex. www.hackaday.com)
and yes, dns.lookup(host, console.log) works and the returned ip address is correct.
Am I right that it works with
x.x.x.x is the correct IP returned.
Thanks. You didn’t mention what version of glibc you have installed but you’re probably hitting a bug in that library. www.hackaday.com is a good example — it’s a CNAME for hackaday.com and older glibc versions didn’t handle AI_V4MAPPED for such records.
As a workaround, try http.get(< family: 4, . >, cb) , that tells node not to use AI_V4MAPPED. Use < family: 6 >if you want an IPv6 connection.
I’m using uclibc 1.0.12. At the moment I’m compiling buildroot with standard glibc. Will tell you if something has changed.
Setting the family to 4 is working. 6 is giving me back the known errors.
So, current workaround will be this:
I cannot compile buildroot with standard glibc. There are too many errors.
So for now, above workaround will be the best solution for me.
Glad it’s working for you now. I’ll close the issue.
I just saw your message regarding the uClibc «bug». Thanks a lot. Will
try to rebuild and test it tomorrow.
Regards and have a nice weekend,
Michael
2016-03-11 13:00 GMT+01:00 Peter Korsgaard notifications@github.com:
You’re welcome. Notice that there hasn’t been any new uclibc-ng releases with this fix and it hasn’t been integrated into the Buildroot yet. I’ll most likely do so this weekend.
I noticed it before and thought to include the current rev version. But
now, I will wait until your release the new official version. Thanks a lot
again. Greets from Liechtenstein.
2016-03-11 21:02 GMT+01:00 Peter Korsgaard notifications@github.com:
You’re welcome. Notice that there hasn’t been any new uclibc-ng releases
with this fix and it hasn’t been integrated into the Buildroot yet. I’ll
most likely do so this weekend.—
Reply to this email directly or view it on GitHub
#5436 (comment).
/cc @indutny — perhaps lib/dns.js should only set AI_V4MAPPED when family == 6?
I just downloaded the newest buildroot and compiled it. My issue is now
fixed. Again, thanks a lot!
uclibc version 1.0.13 has now fixed the http.get issue.
2016-03-11 21:02 GMT+01:00 Peter Korsgaard notifications@github.com:
You’re welcome. Notice that there hasn’t been any new uclibc-ng releases
with this fix and it hasn’t been integrated into the Buildroot yet. I’ll
most likely do so this weekend.—
Reply to this email directly or view it on GitHub
#5436 (comment).