ECONNREFUSED on NodeJS 18
See original GitHub issueWhen 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 |
Issue Analytics
- State:
- Created a year ago
- Reactions:7
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I started seeing this and the only solution I found was to use the IP
127.0.0.1
instead oflocalhost
. I’m not sure whyCan 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
to127.0.0.1
fixes the issue.OS: NixOS, Linux. Node-fetch: 2.6.7