axios: Axios returns ECONNREFUSED 127.0.0.1:80 but port is set to 8080

Describe the bug

Axios returns ECONNREFUSED 127.0.0.1:80 but port is set to 8080

To Reproduce

While having an express server returning 200 on http://localhost:8080/ (and confirmed via browser), running the following returns the error provided (see additional context)

axios({
  headers: { Accept: 'text/html, application/json, text/plain, */*' },
  proxy: undefined,
  url: 'http://localhost:8080',
  method: 'get'
});

Expected behavior

Axios sends an http request to the port specified in the url

Environment

Im indirectly using axios@0.21.1 (start-server-and-test -> wait-on -> axios@0.21.1).

  • Axios Version [0.21.1
  • Adapter HTTP
  • Node.js Version v12.8.0 and v15.8.0
  • OS: macOS 10.15.6

Additional context/Screenshots

Error:

Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 80,
  config: {
    url: 'http://localhost:8080',
    method: 'get',
    headers: {
      Accept: 'text/html, application/json, text/plain, */*',
      'User-Agent': 'axios/0.21.1',
      host: 'localhost:8080'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    httpsAgent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object],
      requests: {},
      sockets: {},
      freeSockets: {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      maxCachedSessions: 100,
      _sessionCache: [Object]
    },
    validateStatus: [Function: validateStatus],
    data: undefined
  },
  request: Writable {
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 0,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      bufferedRequestCount: 0,
      corkedRequestsFree: [Object]
    },
    writable: true,
    _events: [Object: null prototype] {
      response: [Function: handleResponse],
      error: [Function: handleRequestError]
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    _options: {
      maxRedirects: 21,
      maxBodyLength: 10485760,
      protocol: 'http:',
      path: 'http://localhost:8080/',
      method: 'GET',
      headers: [Object],
      agent: undefined,
      agents: [Object],
      auth: undefined,
      hostname: null,
      port: null,
      host: null,
      beforeRedirect: [Function: beforeRedirect],
      nativeProtocols: [Object],
      pathname: 'http://localhost:8080/'
    },
    _ended: true,
    _ending: true,
    _redirectCount: 0,
    _redirects: [],
    _requestBodyLength: 0,
    _requestBodyBuffers: [],
    _onNativeResponse: [Function],
    _currentRequest: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [Socket],
      connection: [Socket],
      _header: 'GET http://localhost:8080/ HTTP/1.1\r\n' +
        'Accept: text/html, application/json, text/plain, */*\r\n' +
        'User-Agent: axios/0.21.1\r\n' +
        'host: localhost:8080\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      path: 'http://localhost:8080/',
      _ended: false,
      res: null,
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      _redirectable: [Circular],
      [Symbol(isCorked)]: false,
      [Symbol(outHeadersKey)]: [Object: null prototype]
    },
    _currentUrl: 'http:http://localhost:8080/'
  },
  response: undefined,
  isAxiosError: true,
  toJSON: [Function: toJSON]
}```

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 35 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I too have the very same issue as reported here. I “think” i know “kindof” why.

Like the other examples here, i was using a http://localhost:8080 or structured similarly URL. This gave me a connection refused which is actually giving a hint:

Error: connect ECONNREFUSED ::1:8080

While i did not in any place or location specify IPv6, the error does indicate an IPv6 call, not v4! In my case changing localhost to 127.0.0.1 did “fix” it.

Debugging this further with ping localhost showed me that magical ::1 too. It was an ipv6 ping. I haven’t quite figured it out yet but i’m guessing there is an error in the users /etc/hosts file where localhost is missing. I’ll report back if i know more about that.

Edit. Don’t know… It is something with the hosts and something with ipv6 but i don’t know what triggers this. Using the ip instead does work but feels wrong.

Try using the local ip of your computer instead of localhost. image

127.0.0.1 add this instead of localhost.

http://localhost:8080 => http://127.0.0.1:8080

For anyone wondering why this is happening, Node.js switched the lookup order of DNS resolution from v17 onwards.

You can switch back to the old behaviour (putting IPv4 first) with:

import { setDefaultResultOrder } from "dns";
setDefaultResultOrder("ipv4first");

Otherwise, yes explicitly using IPv4 127.0.0.1 instead of localhost (which can get resolved to either IPv4 loopback 127.0.0.1 or IPv6 loopback ::1) should work.

I recently have this issue with Axios and the solution was to check my URL. It seems that when you are calling an URL on local without http:// or https:// , Axios tried to connect to localhost.

using my ipv4 address fixed it. Thanks, @Danil27!

If you’re using environment variables, check the route.

In my case, I mistakenly commented my: //require(‘dotenv’).config();

My route was underfined. process.env.MY_ROUTE

i had exactly same issue, I checked my ‘/etc/hosts’ and saw ‘::1 localhost’, removing that line fixed my issue

Don’t do or even recommend that!

It’s your localhost lookup. Your pc in IPv6 now doesn’t know it’s localhost anymore. Which is, frankly, probably fine as not much - if any - is using it. Still, it’s very bad practice to delete that line.

I found it. I specified wrong value in jwksUri field

Thanks @malickshola !

Similar scenario here, I’m using Axios to make a request to my AWS load balancer DNS. Axios does not append the http:// to the URL so I kept getting 200 but empty json response. Fixing my request URL fixed my issue.

I too have the very same issue as reported here. I “think” i know “kindof” why.

Like the other examples here, i was using a http://localhost:8080 or structured similarly URL. This gave me a connection refused which is actually giving a hint:

Error: connect ECONNREFUSED ::1:8080

While i did not in any place or location specify IPv6, the error does indicate an IPv6 call, not v4! In my case changing localhost to 127.0.0.1 did “fix” it.

Debugging this further with ping localhost showed me that magical ::1 too. It was an ipv6 ping. I haven’t quite figured it out yet but i’m guessing there is an error in the users /etc/hosts file where localhost is missing. I’ll report back if i know more about that.

Edit. Don’t know… It is something with the hosts and something with ipv6 but i don’t know what triggers this. Using the ip instead does work but feels wrong.

i had exactly same issue, I checked my ‘/etc/hosts’ and saw ‘::1 localhost’, removing that line fixed my issue

edit: as markg85 suggests, do not delete “::1 localhost”. Thanks markg85 for pointing out

i don’t know about this, but it seems like the _currentURL is built wrong, _currentUrl: 'http:http://localhost:8080/'. It’s doubled, can you try using it without http://

I have same error but in Node, I remove proxy: undefined, and works.

I tried with proxy: false and this make error too