Skip to content

ECONNREFUSED on NodeJS 18 #1624

@unional

Description

@unional

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions