npm install keeps dropping the connection with ECONNRESET
npm ERR! code ECONNRESETShort answer
ECONNRESET means the other end of the connection closed it while npm was still reading. For npm that is the registry, or a proxy sitting in front of it. First run npm ping. If it fails, look at the proxy and registry settings in your npm config. If it passes, the link is unstable: raise the retry and timeout settings.
Fix it fast
1Find out which cause you have
Ask npm to reach the registry it is configured to use. A failure here points at the proxy or registry setting; a pass points at a flaky connection.
npm ping npm config get registry2Match what you see, then jump to its fix
- 1
npm ping fails, and npm config get proxy or https-proxy prints a URL you do not rememberA proxy is set in npm config and it resets the connectionFix - 2
npm config get registry prints something other than https://registry.npmjs.org/The registry setting points at a mirror or host that is downFix - 3
npm ping passes, yet npm install fails part-way and fails at a different package each timeThe connection is unstable and npm gives up too earlyFix
- 1
What does the error look like?
npm ERR! code ECONNRESETnpm ERR! network read ECONNRESETnpm ERR! network This is a problem related to network connectivity.
Cause 1 of 3
A proxy is set in npm config and it resets the connection
1Check
npm sends HTTP and HTTPS requests through the proxy and https-proxy settings, and also honours the HTTP_PROXY and HTTPS_PROXY environment variables. An old or wrong proxy address left in .npmrc is a common leftover.
npm config get proxy
npm config get https-proxy
npm config get noproxy2Fix
If you are not meant to use a proxy, remove both settings. If you are, correct the address, and list hosts that must skip it in noproxy.
npm config delete proxy
npm config delete https-proxy3Verify
npm can reach the registry again.
npm pingConfirmed by docs.npmjs.com
Cause 2 of 3
The registry setting points at a mirror or host that is down
1Check
The registry setting is the base URL for every package request. A company mirror, or a mirror left behind by an earlier setup, that drops connections will fail the same way.
npm config get registry2Fix
Set the registry back to the default, or to a mirror that is working.
npm config set registry=https://registry.npmjs.org/3Verify
npm ping reports a PONG with a response time.
npm pingConfirmed by docs.npmjs.com
Cause 3 of 3
The connection is unstable and npm gives up too early
1Check
Node.js describes ECONNRESET as a connection forcibly closed by the peer, usually from a lost connection, timeout or reboot on the remote socket. By default npm retries a failed fetch only 2 times. On a weak link that is not enough.
npm config get fetch-retries
npm config get fetch-timeout
npm config get maxsockets2Fix
Allow more retries with longer waits between them. Lowering maxsockets (default 15) also opens fewer parallel connections, which helps on a link that drops under load.
npm config set fetch-retries=5
npm config set fetch-retry-mintimeout=20000
npm config set fetch-retry-maxtimeout=120000
npm config set maxsockets=53Verify
Run the install again.
npm installConfirmed by docs.npmjs.com
How do I stop it coming back?
- Keep proxy and registry settings in the project .npmrc only when the team really needs them, and review them when you change network.
- On CI, set the retry and timeout values once in the pipeline config so a brief network blip does not fail a build.
- Run npm ping first when an install fails: it separates a network problem from a package problem in seconds.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
npm install times out or never finishes
npm install does not finish and prints nothing for minutes
npm install waits for the registry, retries a few times, then gives up.
4 causes, each with a fixBack end
npm cannot reach the registry from behind a proxy
ECONNREFUSED
npm needs to be told about your proxy.
3 causes, each with a fixBack end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fix