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.
Fixes npm ERR! 502 Bad Gateway
curl -s https://status.npmjs.org/api/v2/status.json | jq
# Or visit
open https://status.npmjs.orgcurl -s https://status.npmjs.org/api/v2/status.json | jq# Or visitopen https://status.npmjs.orgnpm ERR! 502 Bad Gateway
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.
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 packagesnpm 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.