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:
- 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 |
When using
node-fetchon NodeJS 18, got ECONNREFUSEDSteps to reproduce the behavior:
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