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.
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.
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:[email protected]:8080
npm config set https-proxy http://username:[email protected]: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.crt### 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/npm ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"
npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name: tag names cannot contain spaces
How to fix "npm ERR! code EINVALIDTAGNAME - tag names cannot contain spaces"
npm ERR! code E400 npm ERR! 400 Bad Request
How to fix "npm ERR! code E400 - 400 Bad Request" error