question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ECONNREFUSED on NodeJS 18

See original GitHub issue

When using node-fetch on NodeJS 18, got ECONNREFUSED

// test.mjs
import { Server } from '@hapi/hapi'
import fetch from 'node-fetch'

const start = async () => {
  const server = new Server({
    port: 3344,
  })

  server.route([
    {
      method: 'GET',
      path: '/',
      handler: async () => {
        return JSON.stringify({ value: 'hello' })
      }
    }
  ])
  await server.start()


  const response = await fetch('http://localhost:3344')
  console.log('response', await response.text())
}

start()

Steps to reproduce the behavior:

  1. Run the above in Node 18.7.0. It fails with:
FetchError: request to http://localhost:3344/ failed, reason: connect ECONNREFUSED ::1:3344
    at ClientRequest.<anonymous> (file:///D:/code/mocktomata/node_modules/node-fetch/src/index.js:108:11)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:481: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',
  erroredSysCall: 'connect'
}

Expected behavior

The same script will work in NodeJS 16.

Video repro: https://youtu.be/PgyYPw4RCoI

Your Environment

software version
node-fetch 2.6.7, 3.2.10
node 18.7.0
@hapi/hapi 20.2.2
Operating System Windows

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
tnwannacommented, Aug 31, 2022

I started seeing this and the only solution I found was to use the IP 127.0.0.1 instead of localhost. I’m not sure why

3reactions
danielstaleinycommented, Sep 11, 2022

Can confirm this issue on Node version v18.8.0. I am getting the very same error on different port 8080. Calling with curl works, calling from node results in ECONNREFUSED as described above.

I can also confirm that the very same code works on node v16.17.0. I can also confirm that changing localhost to 127.0.0.1 fixes the issue.

OS: NixOS, Linux. Node-fetch: 2.6.7

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Error: connect ECONNREFUSED - Stack Overflow
ECONNREFUSED error means that connection could not be made with the target service (in your case localhost: ...
Read more >
Node.js Error: connect ECONNREFUSED [Solved] | bobbyhadz
The Node.js Error: connect ECONNREFUSED occurs when a connection to the server could not be established. To solve the error, make sure your...
Read more >
connect ECONNREFUSED 127.0.0.1:3306 solved in Node JS ...
How to fix Error: connect ECONNREFUSED 127.0.0.1:3306 in node js and mysql is shown.
Read more >
Errors | Node.js v19.3.0 Documentation
ECONNREFUSED (Connection refused): No connection could be made because the target machine actively refused it. This usually results from trying to connect to...
Read more >
Connect ECONNREFUSED 127.0.0.1:27017 - MongoDB
I'm receiving error connect ECONNREFUSED 127.0.0.1:27017 randomly today ... .0"},"platform":"Node.js v16.5.0, LE (unified)|Node.js v16.5.0, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found