How to fix npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED npm ERR! FetchError: request to https://registry.npmjs.org/ failed, reason: connect ECONNREFUSED in npm
This error occurs when npm cannot establish a network connection to the npm registry. Common causes include proxy or firewall blocking, VPN interference, misconfigured network settings, or the registry being temporarily unreachable.
Quick fix
Fixes ECONNREFUSED
# Test DNS resolution
nslookup registry.npmjs.org
# Test HTTPS connectivity
curl -I https://registry.npmjs.org/- Check basic network connectivity
- Check and clear proxy settings
- Configure proxy if you need one
- Disable VPN temporarily
# Test DNS resolutionnslookup registry.npmjs.org# Test HTTPS connectivitycurl -I https://registry.npmjs.org/npm ERR! code ECONNREFUSEDnpm ERR! errno ECONNREFUSEDnpm ERR! FetchError: request to https://registry.npmjs.org/ failed, reason: connect ECONNREFUSED
What this error means
The ECONNREFUSED error in npm indicates that the network connection to the npm registry was actively refused. This is a TCP-level error that occurs when npm attempts to connect to a server, but no process is listening on the target port, or a firewall/proxy is blocking the connection. When you run `npm install` or any command that requires fetching packages, npm tries to connect to the registry (by default https://registry.npmjs.org/). If this connection is refused, you'll see ECONNREFUSED along with the IP address and port that npm tried to reach. This error is particularly common in corporate environments with strict firewalls, when using VPNs that interfere with DNS resolution, in Docker containers with network isolation, and in WSL2 environments that have a separate virtual network stack.
Typical symptoms
- npm install fails immediately with ECONNREFUSED error
- Error message shows 'connect ECONNREFUSED 127.0.0.1:443' or similar IP addresses
- Commands that worked previously suddenly fail after network changes
- npm commands fail only on specific networks (corporate, VPN) but work on others
- Docker builds fail at npm install step with connection refused errors
- WSL2 npm commands fail while Windows npm works fine
Common causes
- Proxy or firewall blocking outbound HTTPS connections to registry.npmjs.org
- VPN software interfering with DNS resolution or routing
- Incorrect proxy configuration in npm or environment variables
- Corporate network requiring proxy authentication
- Docker container running with restricted network mode
- WSL2 network isolation issues (separate virtual network from Windows)
- ISP or country-level restrictions on npm registry
- npm registry URL misconfigured to localhost or wrong address
- SSL/TLS inspection by corporate security tools
- DNS resolution failing and returning localhost (127.0.0.1)
How to fix "npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED npm ERR! FetchError: request to https://registry.npmjs.org/ failed, reason: connect ECONNREFUSED"
First, verify you can reach the npm registry at all:
# Test DNS resolution
nslookup registry.npmjs.org
# Test HTTPS connectivity
curl -I https://registry.npmjs.org/If nslookup returns 127.0.0.1 or fails, you have a DNS issue. If curl fails with connection refused, the problem is network-level.
Also check if other HTTPS sites are accessible:
curl -I https://google.com/If this also fails, the issue is with your general internet connectivity, not npm specifically.
Incorrect proxy settings are a common cause. Check your current npm proxy configuration:
npm config get proxy
npm config get https-proxyIf these return values but you're not behind a proxy, remove them:
npm config delete proxy
npm config delete https-proxyAlso check for environment variables that might be setting a proxy:
# Linux/macOS
echo $HTTP_PROXY
echo $HTTPS_PROXY
echo $http_proxy
echo $https_proxy
# Windows PowerShell
echo $env:HTTP_PROXY
echo $env:HTTPS_PROXYUnset any incorrect proxy environment variables for your current session:
# Linux/macOS
unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy
# Windows PowerShell
$env:HTTP_PROXY = ""
$env:HTTPS_PROXY = ""If you're behind a corporate proxy, you need to configure npm to use it:
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080If your proxy requires authentication:
npm config set proxy http://username:password@proxy.company.com:8080
npm config set https-proxy http://username:password@proxy.company.com:8080Note: Special characters in passwords must be URL-encoded. For example, @ becomes %40, # becomes %23.
Ask your IT department for the correct proxy settings if you're unsure.
VPN software often interferes with npm connections. Try temporarily disconnecting your VPN and running npm again:
# Disconnect VPN, then:
npm installIf this works, your VPN is interfering. Solutions include:
1. Configure split tunneling to exclude npm registry traffic
2. Ask your IT department to whitelist registry.npmjs.org
3. Use npm while disconnected from VPN when possible
Check that npm is configured to use the correct registry:
npm config get registryThe default should be https://registry.npmjs.org/. If it shows a different URL (especially localhost or an internal server), reset it:
npm config set registry https://registry.npmjs.org/Also check your .npmrc files for incorrect registry settings:
# Check user config
cat ~/.npmrc
# Check project config (if exists)
cat .npmrcCorrupted cache can sometimes cause connection issues:
npm cache clean --forceThen try your npm command again.
If HTTPS is being blocked or interfered with, you can temporarily use HTTP:
npm config set registry http://registry.npmjs.org/Warning: This is insecure and should only be used as a temporary diagnostic step. Revert to HTTPS once you identify the root cause:
npm config set registry https://registry.npmjs.org/If SSL/TLS inspection by corporate security tools is causing issues:
npm config set strict-ssl falseWarning: This reduces security and should only be used in trusted corporate environments. A better solution is to configure npm to trust your corporate CA certificate:
npm config set cafile /path/to/corporate-ca.crtAdvanced notes
### Docker Container Network Issues
If you're getting ECONNREFUSED inside Docker containers, the container might have restricted network access:
# Use host network mode during build (not recommended for production)
docker build --network host -t myapp .Or configure the container to use the host's DNS:
# docker-compose.yml
services:
app:
dns:
- 8.8.8.8
- 8.8.4.4### WSL2 Network Issues
WSL2 runs in a separate virtual network. If npm fails in WSL2 but works in Windows:
1. Check if Windows firewall is blocking WSL2 network:
# Run in PowerShell as Administrator
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow2. Use Windows DNS servers in WSL2:
# In WSL2, edit /etc/resolv.conf
sudo rm /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf3. Prevent WSL from auto-generating resolv.conf:
# Create /etc/wsl.conf
[network]
generateResolvConf = false### CI/CD Pipeline Issues
In CI environments, ECONNREFUSED often indicates:
1. Rate limiting: npm may temporarily block IPs that make too many requests
2. Network restrictions: CI runners may have limited outbound access
3. Caching proxy issues: Some CI systems use npm caching proxies that fail
Solutions:
- Use npm ci instead of npm install for faster, more reliable installs
- Configure a private npm registry or caching proxy (Verdaccio, Nexus, Artifactory)
- Add retry logic to your CI scripts
### Debugging Connection Issues
Get detailed information about what npm is trying to connect to:
npm install --verbose 2>&1 | grep -E "(ECONNREFUSED|connect|proxy)"Check npm's debug log for more details:
cat ~/.npm/_logs/*-debug.log | tail -100### Alternative Registries
If the main npm registry is blocked in your region, consider using a mirror:
# China - Taobao mirror
npm config set registry https://registry.npmmirror.com/
# Yarn registry (also works with npm)
npm config set registry https://registry.yarnpkg.com/Remember to switch back to the official registry when the network issue is resolved:
npm config set registry https://registry.npmjs.org/Frequently asked questions
- What does "ECONNREFUSED" mean?
- The ECONNREFUSED error in npm indicates that the network connection to the npm registry was actively refused. This is a TCP-level error that occurs when npm attempts to connect to a server, but no process is listening on the target port, or a firewall/proxy is blocking the connection. org/).
- How do I fix "ECONNREFUSED"?
- This error occurs when npm cannot establish a network connection to the npm registry. Common causes include proxy or firewall blocking, VPN interference, misconfigured network settings, or the registry being temporarily unreachable.
- What causes "ECONNREFUSED"?
- Proxy or firewall blocking outbound HTTPS connections to registry.npmjs.org VPN software interfering with DNS resolution or routing Incorrect proxy configuration in npm or environment variables
- Is the "ECONNREFUSED" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐npm Official Documentation
- ๐npm config proxy settings - npm Docs(Official Docs)
- ๐Resolving npm errors - npm Docs(Official Docs)
- ๐ฌnpm ERR! code ECONNREFUSED - Stack Overflow(Stack Overflow)
- ๐ฌnpm install fails with unable to connect - Stack Overflow(Stack Overflow)
- ๐