yarn install fails with AggregateError and ECONNREFUSED
AggregateError [ECONNREFUSED]Short answer
Node.js tries every address a host name resolves to, IPv6 and IPv4, and when all of them refuse the connection it raises one AggregateError. During yarn install that means nothing accepted a connection at the address yarn was told to use. In practice it is a proxy setting that points nowhere or a registry set to a server that is down.
Fix it fast
1Find out which cause you have
Print the proxy and registry lines from every yarn config file that applies. A local address, or a port nothing listens on, is the cause. The AggregateError lists one failed attempt per address, so read the addresses it prints too.
grep -n -i -E "proxy|registry" .yarnrc.yml ~/.yarnrc.yml .yarnrc ~/.yarnrc 2>/dev/null2Match what you see, then jump to its fix
What does the error look like?
AggregateError [ECONNREFUSED]
Cause 1 of 2
A proxy is configured and nothing listens there
1Check
In Yarn 2 and newer the httpProxy and httpsProxy settings in .yarnrc.yml make every request go through that proxy. If the proxy program is stopped, every connection to it is refused.
grep -n -i "proxy" .yarnrc.yml ~/.yarnrc.yml2Fix
Delete the proxy lines if you do not need them, or start the proxy and point yarn at its real address.
# .yarnrc.yml: remove these lines, or fix the address
httpProxy: "http://proxy:4040"
httpsProxy: "http://proxy:4040"3Verify
Run the install again.
yarn installConfirmed by yarnpkg.com
Cause 2 of 2
The registry is set to a server that is not running
1Check
The default npmRegistryServer in Yarn 2 and newer is https://registry.yarnpkg.com. A project or user file can replace it with an address that is down, such as a local mirror.
grep -n -i "npmRegistryServer" .yarnrc.yml ~/.yarnrc.yml2Fix
Delete the override or set the public registry again.
# .yarnrc.yml
npmRegistryServer: "https://registry.yarnpkg.com"3Verify
The install now runs.
yarn installConfirmed by yarnpkg.com
How do I stop it coming back?
- Keep proxy and registry settings in the project file only when the project needs them, and review them after a network change.
- Print yarn config values at the start of a CI job that fails on network errors.
- Do not leave a local proxy setting in the user-level yarn file after a debugging session.
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
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix