How to fix npm ERR! 502 Bad Gateway in npm
The HTTP 502 Bad Gateway error occurs when an intermediate server (like a CDN or proxy) receives an invalid response from the npm registry. This is typically a temporary infrastructure issue.
Quick fix
Fixes npm ERR! 502 Bad Gateway
curl -s https://status.npmjs.org/api/v2/status.json | jq
# Or visit
open https://status.npmjs.org- Check npm status
- Retry the request
- Try different CDN endpoint
- Use npm mirror
curl -s https://status.npmjs.org/api/v2/status.json | jq# Or visitopen https://status.npmjs.orgnpm ERR! 502 Bad Gateway
What this error means
HTTP 502 indicates that a gateway or proxy server received an invalid response from an upstream server. In npm's case, this usually means the CDN or load balancer couldn't get a proper response from the actual registry servers. This is almost always a temporary infrastructure issue and not something you can fix directly. It often affects only certain geographic regions or CDN edge locations.
Typical symptoms
- npm install fails with 502 error
- Specific packages fail while others work
- Error is region-specific
- Intermittent failures
- Browser access to registry works
- VPN changes affect the error
Common causes
- CDN edge server issues
- Registry backend server unreachable
- Load balancer problems
- Network routing issues
- Registry infrastructure maintenance
- DDoS mitigation affecting traffic
- Geographic routing problems
How to fix "npm ERR! 502 Bad Gateway"
Look for known issues:
curl -s https://status.npmjs.org/api/v2/status.json | jq
# Or visit
open https://status.npmjs.org502 errors are often transient:
# Simple retry
npm install
# With delay
for i in 1 2 3; do npm install && break || sleep 30; doneChange DNS resolution path:
# Use different DNS
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
# Or use VPN to change geographic routingTemporarily switch registry:
npm install --registry https://registry.npmmirror.comThis bypasses the main npm CDN.
Identify where the issue is:
# Trace the route
traceroute registry.npmjs.org
# Test direct connection
curl -v https://registry.npmjs.org/lodashWork offline if possible:
# If packages are in cache
npm install --prefer-offline
# Or use local proxy with cached packagesAdvanced notes
npm uses Fastly CDN which has global edge locations. 502 errors often indicate issues at specific edge nodes. Using a VPN to appear from a different region can sometimes route around problem nodes. For production systems, maintain a warm package cache and consider using a caching proxy. Monitor npm status via their API for automated alerting.
Frequently asked questions
- What does "npm ERR! 502 Bad Gateway" mean?
- HTTP 502 indicates that a gateway or proxy server received an invalid response from an upstream server. In npm's case, this usually means the CDN or load balancer couldn't get a proper response from the actual registry servers. This is almost always a temporary infrastructure issue and not something you can fix directly.
- How do I fix "npm ERR! 502 Bad Gateway"?
- The HTTP 502 Bad Gateway error occurs when an intermediate server (like a CDN or proxy) receives an invalid response from the npm registry. This is typically a temporary infrastructure issue.
- What causes "npm ERR! 502 Bad Gateway"?
- CDN edge server issues Registry backend server unreachable Load balancer problems
- Is the "npm ERR! 502 Bad Gateway" 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
- ๐
- ๐