lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.66k stars 102 forks source link

localhost is not resolving to 127.0.0.1 #152

Closed dmtrKovalenko closed 1 year ago

dmtrKovalenko commented 1 year ago

A simple example of

import fetch from "cross-fetch";

fetch("http://localhost:60002/graphql")
  .then(res => {
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    return res.json();
  })
  .then(console.log)
  .catch(console.log);

Returns ECONNREFUSED

FetchError: request to http://localhost:60002/graphql failed, reason: connect ECONNREFUSED ::1:60002
    at ClientRequest.<anonymous> (/Users/dmtrkovalenko/dev/lightsource/www/node_modules/cross-fetch/node_modules/node-fetch/lib/index.js:1491:11)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:496:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED'
}

But when I change localhost to 127.0.0.1 it works. curl and any other native api's works either with localhost or 127.0.0.1.

System: MacOS Ventura 13.1 Platform: Apple silicon cross-fetch version: 3.1.5 node: 18.13.0

lquixada commented 1 year ago

This seems to be an issue with local DNS lookup. Check if an entry like this 127.0.0.1 localhost exists on your /etc/hosts file. If the problem persists, please reach out to node-fetch repo to ask for help since cross-fetch just acts as proxy.