The HTTP 504 Gateway Timeout error occurs when the npm registry doesn't respond in time. This indicates network latency issues, registry overload, or proxy timeout configurations.
Fixes npm ERR! 504 Gateway Timeout
# Test latency
ping registry.npmjs.org
# Test download speed
curl -o /dev/null -w "%{speed_download}" https://registry.npmjs.org/lodash# Test latencyping registry.npmjs.org# Test download speedcurl -o /dev/null -w "%{speed_download}" https://registry.npmjs.org/lodashnpm ERR! 504 Gateway Timeout
HTTP 504 indicates that a gateway or proxy didn't receive a timely response from the upstream server. In npm's context, this means the CDN or intermediate proxy timed out waiting for a response from the registry backend. This can be caused by slow network connections, registry overload, or aggressive timeout settings on intermediate proxies.
Check latency and throughput:
# Test latency
ping registry.npmjs.org
# Test download speed
curl -o /dev/null -w "%{speed_download}" https://registry.npmjs.org/lodashAllow more time for slow connections:
npm config set fetch-timeout 600000
npm config set fetch-retry-mintimeout 20000
npm config set fetch-retry-maxtimeout 120000Fewer parallel downloads:
npm config set maxsockets 3This reduces load on slow connections.
Reduce latency with regional mirror:
# For China
npm config set registry https://registry.npmmirror.com
# For Europe (use company mirror if available)If behind corporate proxy:
1. Request IT increase proxy timeout
2. Typical timeout should be 5+ minutes
3. Or use local npm proxy that has better timeout handling
For persistently failing packages:
# Install problematic package separately
npm install large-package --timeout 600000
# Then install rest
npm installIn CI/CD on cloud providers, choose regions with good connectivity to npm CDN (US-East typically best). For air-gapped or high-latency environments, sync packages to a local registry. Consider using pnpm which has more efficient network usage. Monitor npm network requests to identify consistently slow packages.