6

The following code produces an exception in node.js under windows:

var Socket = require("net").Socket;

socket = new Socket();
socket.connect(80, "localhost");

here's the message:

    events.js:2083: Uncaught Error: getHostByName ENOTFOUND

When I remove localhost, it works fine. What could be causing this? I tried turning the firewall off, but to no effect.

Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
Corno
  • 5,448
  • 4
  • 25
  • 41
  • Possible duplicate of [Mongoose Can't Connect Without Internet](https://stackoverflow.com/questions/29178484/mongoose-cant-connect-without-internet) – Yaacov Oct 20 '17 at 12:51

3 Answers3

9

node.js uses c-ares resolver and ignores system resolver completely. So the advices regarding C:\WINDOWS\system32\drivers\etc\hosts are probably irrelevant.

c-ares library reads certain system config files - e.g. on Windows and Cygwin it reads /etc/resolv.conf. So you should check if it reads /etc/hosts or C:\WINDOWS\system32\drivers\etc\hosts. If it doesn't and doesn't have builtin support for localhost - then you will have to use 127.0.0.1

nponeccop
  • 13,527
  • 1
  • 44
  • 106
  • Is this still the case, since Cygwin is no longer used for Node.js on Windows? – Brad Dec 12 '13 at 04:11
  • 1
    It is not the case, but it's because of a design change unrelated to Cygwin: see http://nodejs.org/api/dns.html. Basically `net.connect` has been changed to use the system resolver instead of c-ares. – nponeccop Dec 12 '13 at 18:16
4

In my case adding the Microsoft Loopback Adaptor fixed this issue.

  1. Click the Start Menu Orb. In the search box Type hdwwiz and then Click the hdwwize.exe program link.
  2. Now the Add Hardware wizard should be open.
  3. Scroll down the list and Select Network Adapters then Click Next.
  4. Give the next window a moment to load, and then Click Microsoft and Select Microsoft Loopback Adapter.

From: http://www.groovypost.com/howto/microsoft/install-a-loopback-adapter-in-windows-7/

Glen Blanchard
  • 926
  • 8
  • 17
-2

Option 1: Dont' use windows ;)

Option 2: in C:\WINDOWS\system32\drivers\etc\hosts

add :

 127.0.0.1       localhost
Cris-O
  • 4,989
  • 3
  • 17
  • 11