node-fetch: ECONNREFUSED on NodeJS 18
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 |
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 33
- Comments: 19 (2 by maintainers)
Commits related to this issue
- use 127.0.0.1 instead of localhost which seems to cause issues in Node.js 18 see https://github.com/node-fetch/node-fetch/issues/1624 — committed to danielpeintner/thingweb.node-wot by danielpeintner 2 years ago
- Fix deps for node 18 - Also using node-fetch with node 18 => cannot use localhost anymore - https://github.com/node-fetch/node-fetch/issues/1624 — committed to sos-lapsikyla/ylitse-app by rottabonus a year ago
- use server address instead of localhost there is a bug in node-fetch that requires to don't use localhost node-fetch/node-fetch#1624 — committed to vercel/vercel by Kikobeats a year ago
- use server address instead of localhost there is a bug in node-fetch that requires to don't use localhost node-fetch/node-fetch#1624 — committed to vercel/vercel by Kikobeats a year ago
- use server address instead of localhost there is a bug in node-fetch that requires to don't use localhost node-fetch/node-fetch#1624 — committed to vercel/vercel by Kikobeats a year ago
- Add an extra step to setup NODE_OPTIONS To add `export NODE_OPTIONS=--dns-result-order=ipv4first` and so that `localhost` resolves to 127.0.0.1 (IPv6) in the case when /etc/hosts also has a mapping f... — committed to serlo/api.serlo.org by eliflores a year ago
- Setting NODE_OPTIONS=--dns-result-order=ipv4first when running yarn pacts In the failures for the Pacts workflow, the error seems related to node not being able to connect to the server running on th... — committed to serlo/database-layer by eliflores a year ago
- Use 127.0.0.1 instead of localhost - https://github.com/cypress-io/cypress/issues/26154 - https://github.com/node-fetch/node-fetch/issues/1624 - https://github.com/nodejs/node/issues/40702 — committed to davidmyersdev/octo by davidmyersdev 9 months ago
- Use 127.0.0.1 instead of localhost - https://github.com/cypress-io/cypress/issues/26154 - https://github.com/node-fetch/node-fetch/issues/1624 - https://github.com/nodejs/node/issues/40702 — committed to davidmyersdev/octo by davidmyersdev 9 months ago
- possibly fixes node ci tool based on https://github.com/node-fetch/node-fetch/issues/1624 — committed to synergylabs/BuildingDepot by sud335 5 months ago
- fix: use 127.0.0.1 instead of localhost for healthcheck for compatibility with node 18. See https://github.com/node-fetch/node-fetch/issues/1624. — committed to gatehill/imposter-js by outofcoffee 5 months ago
- fix: use 127.0.0.1 instead of localhost for healthcheck for compatibility with node 18. See https://github.com/node-fetch/node-fetch/issues/1624. — committed to gatehill/imposter-js by outofcoffee 5 months ago
- fix: use 127.0.0.1 instead of localhost for healthcheck for compatibility with node 18. See https://github.com/node-fetch/node-fetch/issues/1624. — committed to gatehill/imposter-js by outofcoffee 5 months ago
- fix: use 127.0.0.1 instead of localhost for healthcheck for compatibility with node 18. See https://github.com/node-fetch/node-fetch/issues/1624. — committed to gatehill/imposter-js by outofcoffee 5 months ago
I started seeing this and the only solution I found was to use the IP
127.0.0.1instead oflocalhost. I’m not sure whythere was a breaking change in node v17 that changed the default IP resolving. ip6 is preferred by default. You can add
dns.setDefaultResultOrder('ipv4first')at the beginning of you app and that should fix the problem (https://nodejs.org/api/dns.html#dnssetdefaultresultorderorder)This would be since
localhostresolves to::1which is the “equivalent” of 127.0.0.1 but in IPv6 instead of IPv4. Since the server seems to be listening only on IPv4 this means that the connection will be refused when trying::1.I believe that this error exists when using the builtin
httpmodule as well, and probably has to do with how Node.js does dns resolving…If you want to move forward with this I would suggest first seeing if you can reproduce the same issue using the builtin
httpmodule, and if you can, file an issue with Node.js directly. If this cannot be reproduced in the builtinhttpmodule, than it is most likely something in this module that messes this up.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
ECONNREFUSEDas described above.I can also confirm that the very same code works on node v16.17.0. I can also confirm that changing
localhostto127.0.0.1fixes the issue.OS: NixOS, Linux. Node-fetch: 2.6.7
why was this closed? we are still having this issue when using ‘localhost’
Node.js 18+ has built-in
fetch. You just need to remove this package.Changing ‘localhost’ to ‘127.0.0.1’ has fixed the problem for me.
I changed localhost to 127.0.0.1, I don’t know what is the reason but work successfully
Is there anyone having this issue in Ubuntu 20.04.6 with node version 18.19.0?
I have the issue with node v18.17.1. Neither setting ip to 127.0.0.1 or dns.setDefaultResultOrder(‘ipv4first’); fixes the problem.
Indeed it is issue with nodejs. https://github.com/nodejs/node/issues/40702
I confirmed this fixed the issue
works on node 16 but above solution doesn’t work even if the IP were changed to localhost
That worked for me!
I experience the same issue, I use nvm for Windows and same code works properly for 16.17.0
Calling the server using
curlis working, showing it is not a problem of@hapi/hapi:Also, running the server in NodeJS 16.16.0, the
curlcall gives identical result, showing@hapi/hapiworks identically in both environment.