Lingua-e
JavaScript

What is a ECONNRESET?

← Errors

ECONNRESET is a Node.js network error that occurs when a TCP connection is forcibly closed by the remote peer while data is still being transferred. Unlike ECONNREFUSED (connection rejected before it starts), ECONNRESET happens mid-connection.

Why it happens

  • The remote server crashes or restarts while a request is in progress.
  • A load balancer or proxy has a shorter idle timeout than your client.
  • The server sends a TCP RST packet to close the connection abruptly.
  • Network equipment (firewalls, NAT) drops an idle connection.

Examples

JavaScript

Code that triggers the error

const http = require("http");
const req = http.get("http://example.com/large-file", (res) => {
  res.on("data", () => {});
});
// Remote server closes the connection mid-transfer

Error output

Error: read ECONNRESET
    at TLSSocket.onConnectEnd (_tls_wrap.js:...)
    errno: -54,
    code: 'ECONNRESET',
    syscall: 'read'

How to fix it

Implement retry logic with exponential backoff for transient ECONNRESET errors. Set appropriate keep-alive and timeout settings on your HTTP agent. Monitor for patterns (all requests to one server resetting) to distinguish infrastructure issues from code bugs.

Fixed code

const http = require("http");
const req = http.get("http://example.com/large-file", (res) => {
  res.on("data", () => {});
  res.on("error", (err) => console.error("Response error:", err));
});
req.on("error", (err) => {
  if (err.code === "ECONNRESET") {
    console.error("Connection reset by peer, retrying...");
  }
});

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free