Skip to content

Unable to connect to establish a TCP connection (ECONNREFUSED) #40702

Closed as not planned

Description

@JeffreyArt1

Version

v17.0.1

Platform

Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

I'm trying to connect to a micro-service with this piece of code:

const connect = (host, port) => {
  socket = new require('net').Socket();
  socket.connect(port, host)
    .on('connect', () => console.log('konected!'))
    .on('error', e => console.log(e));
};

connect('localhost', 5098);

this prints out:

Error: connect ECONNREFUSED ::1:5098
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 5098
}

How often does it reproduce? Is there a required condition?

Since installed node 17

What is the expected behavior?

No errors.

What do you see instead?

Error: connect ECONNREFUSED ::1:5098
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 5098
}

Additional information

As far a i tested (v14.18.1 and v16.12.0), this only happens in this version of node.

Activity

  1. added
    netIssues and PRs related to the net subsystem.
    on Nov 2, 2021
  2. nsainaney commented on Nov 2, 2021

    @nsainaney

    We have been having the same issue with node 17 and it appears to be a breaking change with how DNS.lookup works. With node 16, the lookup would return a IPv4 address but with node 17, it returns an IPv6 address which will break most REST clients that hardcode URLS like http://localhost:4040/api if the upstream server only binds to the IPv4 address (e.g. server.listen('127.0.0.1'...) etc..

    sudo n latest
       installed : v17.0.1 (with npm 8.1.0)
    ╭─[prompt redacted]›
    ╰─$ node
    Welcome to Node.js v17.0.1.
    Type ".help" for more information.
    > require('dns').lookup('localhost', null, (e,a,f) => console.log(e,a,f))
    GetAddrInfoReqWrap {
      callback: [Function (anonymous)],
      family: 0,
      hostname: 'localhost',
      oncomplete: [Function: onlookup]
    }
    > null ::1 6
    .exitit
    ╭─[prompt redacted]›
    ╰─$ sudo n 16.2.0
       installed : v16.2.0 (with npm 7.13.0)
    ╭─[prompt redacted]›
    ╰─$ node
    Welcome to Node.js v16.2.0.
    Type ".help" for more information.
    > require('dns').lookup('localhost', null, (e,a,f) => console.log(e,a,f))
    GetAddrInfoReqWrap {
      callback: [Function (anonymous)],
      family: 0,
      hostname: 'localhost',
      oncomplete: [Function: onlookup]
    }
    > null 127.0.0.1 4
    

    Notice the results are null ::1 6 vs null 127.0.0.1 4 with null options

  3. richardlau commented on Nov 2, 2021

    @richardlau
    Member

    For reference the change was #39987 -- Node.js no longer re-sorts results of IP address lookups and returns them as-is (i.e. it no longer ignores how your OS has been configured). You can change the behaviour via the verbatim option to dns.lookup() or set the --dns-result-order command line option to change the default.

  4. nsainaney commented on Nov 2, 2021

    @nsainaney

    Thanks, @richardlau. How is one supposed to fix this if they are not the author of the server/client. Libraries like axios do not let you pass in the verbatim option so I suppose we all have to use the command line option until packages catch up.

  5. nsainaney commented on Nov 2, 2021

    @nsainaney

    Update to my comment above. When dealing with 3rd party libraries, we can use dns.setDefaultResultOrder('ipv4first') and have to be careful to use this in child_processes as well

  6. wibobm commented on Nov 11, 2021

    @wibobm

    v17.0.1 and v17.1.0 have same issue with remote debugging

    Error in debuggerConnector: connect ECONNREFUSED ::1:49629
    Error: connect ECONNREFUSED ::1:49629
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)

  7. treysis commented on Nov 12, 2021

    @treysis
    Contributor

    @richardlau The better option would of course be to change localhost to 127.0.0.1 when connecting to legacy localhost.

    @nsainaney If you cannot fix server/client, the solution would be to stay on the support NodeJS version, i.e. <17, until the authors of server/client add compatibility for Node v17.

  8. nsainaney commented on Nov 12, 2021

    @nsainaney

    Thanks @treysis. There are enough workarounds that this isn't an issue for us.

  9. treysis commented on Nov 12, 2021

    @treysis
    Contributor

    @nsainaney Yes, I'm just mentioning it because I consider it to be the cleanest way. If you set the setDefaultResultOrder option you might forget about that in the future and will limit IPv6 capability of your applications.

  10. nsainaney commented on Nov 12, 2021

    @nsainaney

    That's a good point. Thank you

  11. lydell commented on Apr 14, 2022

    @lydell

    I run this:

    require("http").get("http://localhost:3004/", res => console.log(res.statusCode))
    • In Node.js 16 it prints a status code.
    • In Node.js 17 I get ECONNREFUSED.

    Where can I find the OS configuration that Node.js now honors…

    • …on macOS?
    • …on Linux?

    Sorry if it feels like a stupid question or like a question that belongs somewhere else, but I think more people than me don’t know this DNS stuff very well and will wonder why things “aren’t working”.

  12. lexicalunit commented on Apr 14, 2022

    @lexicalunit

    I run this:

    require("http").get("http://localhost:3004/", res => console.log(res.statusCode))

    You have to change this to:

    const dns = require('dns');
    dns.setDefaultResultOrder('ipv4first');
    require("http").get("http://localhost:3004/", res => console.log(res.statusCode))

    Which should work in both Node v16 and v17, as well as on macOS and Linux.

  13. lydell commented on Apr 14, 2022

    @lydell

    @lexicalunit Yes, I read that in the various threads, but that’s not what I’m asking for.

  14. 69 remaining items

  15. treysis commented on Dec 19, 2023

    @treysis
    Contributor

    I still get the error

    Which error exactly and what do you do?

  16. Rakeshbora007 commented on Jan 4, 2024

    @Rakeshbora007

    NEXT_PUBLIC_API_URL=http://127.0.0.1:3000

    it still not working formw

  17. tahmidrahman-dsi commented on Jan 19, 2024

    @tahmidrahman-dsi

    Could anyone find this problem in Node v18.19.0 with Ubuntu 20.04.6?

  18. treysis commented on Jan 19, 2024

    @treysis
    Contributor

    NEXT_PUBLIC_API_URL=http://127.0.0.1:3000

    it still not working formw

    Which Node version? Did you try localhost instead of 127.0.0.1?

  19. treysis commented on Jan 19, 2024

    @treysis
    Contributor

    Could anyone find this problem in Node v18.19.0

    Use Node 20!

  20. 1-Rishav commented on Jan 27, 2024

    @1-Rishav

    Make sure that your password and port number of database should be correct because this "ERROR" take place when your database credentials are wrong.
    Screenshot 2024-01-27 105845

  21. mattdesl commented on Feb 17, 2024

    @mattdesl

    Is the code in the OP supposed to work on node 20?

    const connect = (host, port) => {
      socket = new require('net').Socket();
      socket
        .connect(port, host)
        .on('connect', () => console.log('konected!'))
        .on('error', (e) => console.log(e));
    };
    
    connect('localhost', 5098);

    I get the same error in node v20.11.1 and v21.6.2:

    AggregateError [ECONNREFUSED]: 
        at internalConnectMultiple (node:net:1116:18)
        at afterConnectMultiple (node:net:1680:7) {
      code: 'ECONNREFUSED',
      [errors]: [
        Error: connect ECONNREFUSED 127.0.0.1:5098
            at createConnectionError (node:net:1643:14)
            at afterConnectMultiple (node:net:1673:16) {
          errno: -61,
          code: 'ECONNREFUSED',
          syscall: 'connect',
          address: '127.0.0.1',
          port: 5098
        },
        Error: connect ECONNREFUSED ::1:5098
            at createConnectionError (node:net:1643:14)
            at afterConnectMultiple (node:net:1673:16) {
          errno: -61,
          code: 'ECONNREFUSED',
          syscall: 'connect',
          address: '::1',
          port: 5098
        }
      ]
    }
    

    This also seems related to the error I'm getting from this demo app:
    p2panda/mushroom-app-tutorial#7

  22. lydell commented on Feb 17, 2024

    @lydell

    @mattdesl In your error message, you can see how there are two errors: Node tried both connecting via IPv4 127.0.0.1:5098, and via IPv6 ::1:5098, which is the Happy Eyeballs thing added in #44731.

    I get that error too, because I have nothing running on port 5098. But when I start a server listening on 5098, OP’s code runs without errors:

    require('http').createServer().listen(5098);
    
    // If you do this instead, the below code fails on Node.js 16 but works on later Node.js versions (unsure exactly which releases):
    // require('http').createServer().listen(5098, '::1');
    
    const connect = (host, port) => {
      socket = new require('net').Socket();
      socket
        .connect(port, host)
        .on('connect', () => console.log('konected!'))
        .on('error', (e) => console.log(e));
    };
    
    connect('localhost', 5098);
  23. undergroundwires commented on Mar 30, 2024

    @undergroundwires

    Here's my workaround (open-source and documented) that I hope that can help you too:

    After days of research and trial/error, this is how I got this working:

    1. Create a script called force-ipv4.sh, that configures system to prefer IPv4 over IPv6, call it to configure the machine. It was not easy to find a reliable cross-platform solution and I went Cloudflare WARP for DNS resolution along with some system configurations.
    2. To easily use the script in GitHub workflows, create GitHub action called force-ipv4 and call it in GitHub runners.
    3. Fixes the IPv6 request issues, and you can happily run e.g. fetch API from Node.

    Related commit introducing this fix: undergroundwires/privacy.sexy@52fadcd

  24. added a commit that references this issue on Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    netIssues and PRs related to the net subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions