Troubleshooting 4 min read

Node.js ECONNRESET with a Proxy: Causes and Fixes for axios, fetch and Puppeteer

In Node, ECONNRESET through a proxy usually comes from three places: how axios handles proxies, reused keep-alive sockets, and transient exits. Here's each fix.

Node.js ECONNRESET with a proxy means the TCP connection your request was using got reset by the other side. You'll see it as Error: socket hang up with code ECONNRESET, or as TypeError: fetch failed with cause.code set to ECONNRESET when using the built-in fetch. Through a proxy there are some Node-specific reasons on top of the general ones: how axios implements proxies, keep-alive defaults that changed in recent Node versions, and missing error handling. This guide covers each with code you can drop in.

Find the Real Error Code

Node wraps errors differently depending on the client. Log the right field before guessing:

try {
  // ... your request
} catch (err) {
  // axios and http: err.code; built-in fetch / undici: err.cause.code
  console.log(err.code || (err.cause && err.cause.code), err.message);
}

If the code is ECONNREFUSED or ENOTFOUND instead, you're not reaching the proxy at all: check host global.nodeproxies.xyz and port 8080. If you get an HTTP 407, it's credentials or an inactive plan, not a reset.

Fix 1: Use a Proxy Agent with axios

axios has a built-in proxy option, but for HTTPS targets a dedicated agent that issues a proper CONNECT tunnel is more reliable. Disable the built-in option and pass an agent:

// npm install axios https-proxy-agent
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');

const PROXY = 'http://USERNAME:[email protected]:8080';

async function main() {
  const agent = new HttpsProxyAgent(PROXY);
  const res = await axios.get('https://api.ipify.org', {
    httpsAgent: agent,
    proxy: false, // stop axios applying its own proxy logic or env vars
    timeout: 30000,
  });
  console.log(res.data);
}

main();

Setting proxy: false also stops axios from picking up HTTP_PROXY or HTTPS_PROXY environment variables, which can otherwise route requests through a second, unexpected proxy.

Fix 2: Watch Out for Keep-Alive

Since Node 19, the default global HTTP agent uses keep-alive. Idle sockets stay in a pool, and if the proxy or website closes them while idle, the next request on that socket fails with ECONNRESET. This produces the classic pattern of a script that works, sits idle for a while, and then throws one reset.

  • Retry idempotent requests once on ECONNRESET. The retry opens a new socket.
  • When you rotate IPs per request anyway, create a fresh agent per request or construct it with keepAlive: false so sockets aren't pooled.
  • When you need speed on one site, keep-alive is worth having. Pair it with a sticky session (USERNAME-session-abc123-time-10) and retry on reset.

Fix 3: Built-in fetch Doesn't Use Your Proxy by Default

Node's built-in fetch (undici) doesn't read a proxy option. Use undici's ProxyAgent as a dispatcher:

// npm install undici
const { fetch, ProxyAgent } = require('undici');

const dispatcher = new ProxyAgent({
  uri: 'http://global.nodeproxies.xyz:8080',
  token: 'Basic ' + Buffer.from('USERNAME:PASSWORD').toString('base64'),
});

fetch('https://api.ipify.org', { dispatcher })
  .then((res) => res.text())
  .then(console.log)
  .catch((err) => console.error(err.cause ? err.cause.code : err));

Fix 4: A Retry Wrapper

Resets through rotating residential proxies are usually transient, since exits are real consumer connections. A small wrapper with a fresh agent per attempt handles them:

const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');

const PROXY = 'http://USERNAME:[email protected]:8080';
const RETRYABLE = new Set(['ECONNRESET', 'ETIMEDOUT', 'ECONNABORTED', 'EPIPE']);
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

async function get(url, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    const agent = new HttpsProxyAgent(PROXY); // fresh socket, fresh IP
    try {
      return await axios.get(url, { httpsAgent: agent, proxy: false, timeout: 30000 });
    } catch (err) {
      if (!RETRYABLE.has(err.code) || i === attempts - 1) throw err;
      await sleep(1000 * 2 ** i);
    } finally {
      agent.destroy();
    }
  }
}

get('https://example.com').then((res) => console.log(res.status));

Fix 5: Puppeteer and Playwright

In headless browsers, resets show up as net::ERR_CONNECTION_RESET. Common causes are a missing page.authenticate call in Puppeteer (so the proxy rejects the tunnel), SOCKS5 with credentials (Chromium doesn't support it; use the HTTP endpoint), and targets that reset connections from automated browsers. Our Node.js, Axios and Puppeteer guide has working launch code.

Fix 6: Handle Socket Errors So the Process Doesn't Crash

With the low-level http and https modules, an unhandled 'error' event on a request crashes the process. Always attach req.on('error', ...) or use a promise-based client. A crash on one reset can take down a long job that would otherwise have recovered on retry.

When It's Not the Proxy

If resets only happen on one site and always during the TLS handshake, that site is likely rejecting your client's fingerprint. If they happen on every request on one network only, a firewall is interfering. The general diagnosis is in connection reset through a proxy.

Node.js with NodeProxies

Use http://USERNAME:[email protected]:8080 with an agent, put options like -country-us in the username, and retry resets once. API and integration details are in the docs. Create an account to get credentials.

FAQ

What does socket hang up ECONNRESET mean with a proxy?

The connection was closed by the proxy, the exit or the website before a response arrived. It's often a stale keep-alive socket or a transient exit; retry once with a new connection.

Why does axios give ECONNRESET with HTTPS proxies?

axios' built-in proxy handling can misbehave with HTTPS targets. Use https-proxy-agent as httpsAgent and set proxy: false.

How do I use a proxy with Node fetch?

Pass an undici ProxyAgent as the dispatcher option, with credentials in a Basic token, as shown above.

NodeProxies

Ready to get started?

90M+ residential, mobile & datacenter proxies. Datacenter from $0.30/GB, residential from $0.42/GB. No contracts.

Create free account