This error occurs when npm cannot establish a connection to the registry within the timeout period (default 30 seconds). Common causes include IPv6 connectivity issues, proxy misconfiguration, firewall restrictions, or slow network conditions.
The ETIMEDOUT error in npm indicates that a network request to the npm registry (or a private registry) failed because it took too long to complete. By default, npm waits 30 seconds for a response before timing out. When you run `npm install`, npm connects to the registry to fetch package metadata and tarballs. If the connection cannot be established or the response takes too long, you'll see this timeout error. The issue is almost always network-related rather than a problem with npm itself. This error has become increasingly common since Node.js 18+, which defaults to IPv6 connections. Many networks and ISPs don't properly support IPv6, causing connections to hang before falling back to IPv4 - often exceeding the timeout limit. Other causes include corporate proxies, VPN interference, firewall rules blocking outbound connections, or simply slow/unstable internet.
The most common cause of ETIMEDOUT on Node.js 18+ is IPv6 connectivity issues. Force npm to use IPv4:
# Set prefer-ip-version to 4 in npm config
npm config set prefer-ip-version 4Or set it globally via environment variable:
# Linux/macOS - add to ~/.bashrc or ~/.zshrc
export NODE_OPTIONS="--dns-result-order=ipv4first"
# Windows PowerShell
$env:NODE_OPTIONS="--dns-result-order=ipv4first"Then retry your npm command:
npm installIf your network is slow or unreliable, increase the timeout values in your .npmrc:
# Set fetch-retry-maxtimeout to 10 minutes (600000ms)
npm config set fetch-retry-maxtimeout 600000
# Increase fetch-retries for more retry attempts
npm config set fetch-retries 5
# Increase fetch-timeout (time per request)
npm config set fetch-timeout 300000You can also set these directly in your ~/.npmrc file:
fetch-retry-maxtimeout=600000
fetch-retries=5
fetch-timeout=300000If you're behind a corporate proxy, npm needs to be configured to use it:
# Check current proxy settings
npm config get proxy
npm config get https-proxy
# Set proxy (replace with your proxy URL)
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
# If your proxy requires authentication
npm config set proxy http://username:[email protected]:8080
npm config set https-proxy http://username:[email protected]:8080If you're NOT behind a proxy but have stale proxy settings, remove them:
npm config delete proxy
npm config delete https-proxyAlso check for proxy environment variables:
# Check if proxy env vars are set
echo $HTTP_PROXY
echo $HTTPS_PROXY
echo $NO_PROXYCached data can sometimes cause connectivity issues:
# Clear the npm cache
npm cache clean --force
# Delete package-lock.json and node_modules
rm -rf node_modules package-lock.json
# Retry installation
npm installOn Windows:
npm cache clean --force
Remove-Item -Recurse -Force node_modules, package-lock.json
npm installThe audit check adds extra network requests that can timeout. Skip it temporarily:
npm install --no-auditYou can also disable it permanently:
npm config set audit falseSimilarly, reduce maxsockets to limit parallel connections (helpful on unreliable networks):
npm config set maxsockets 3Verify that you can reach the npm registry:
# Test DNS resolution
nslookup registry.npmjs.org
# Test HTTPS connectivity
curl -I https://registry.npmjs.org
# On Windows without curl
Invoke-WebRequest -Uri https://registry.npmjs.org -Method HeadIf DNS fails, try using a different DNS server (e.g., Google's 8.8.8.8 or Cloudflare's 1.1.1.1).
If HTTPS fails but HTTP works, you may have a firewall or proxy blocking HTTPS traffic.
If the main npm registry is slow or unreachable from your location, try a mirror:
# Use the npm mirror (for China/Asia)
npm config set registry https://registry.npmmirror.com
# Or use Yarn's mirror
npm config set registry https://registry.yarnpkg.comTo reset back to the official registry:
npm config set registry https://registry.npmjs.org/VPNs can cause routing issues. Try temporarily disconnecting:
1. Disconnect from your VPN
2. Run npm install again
3. If it works, you may need to configure VPN split tunneling to exclude npm traffic
On macOS, also check if you have multiple network interfaces:
# List network services
networksetup -listallnetworkservicesDisable unused network interfaces that might be causing routing conflicts.
### WSL2 and Docker Networking
In WSL2 or Docker, network issues are common:
WSL2:
# Reset WSL network
wsl --shutdown
# Then restart WSLIf issues persist, check Windows firewall rules aren't blocking WSL network traffic.
Docker:
# Add to your npm install command in Dockerfile
RUN npm install --no-audit --prefer-offline --maxsockets=3Or use a multi-stage build to cache dependencies:
FROM node:20 AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --no-audit### CI/CD Retry Logic
For automated environments, add retry logic:
GitHub Actions:
- name: Install dependencies
run: npm ci --no-audit
env:
NODE_OPTIONS: "--dns-result-order=ipv4first"
timeout-minutes: 10GitLab CI:
install:
script:
- npm ci --no-audit || npm ci --no-audit
variables:
NODE_OPTIONS: "--dns-result-order=ipv4first"### Diagnosing with Verbose Logging
Enable verbose logging to see exactly where the timeout occurs:
npm install --loglevel verboseOr check npm debug logs:
# Find the most recent debug log
ls -la ~/.npm/_logs/
# View the log
cat ~/.npm/_logs/*-debug.log### Corporate Firewall Rules
If you're behind a corporate firewall, ensure these domains are allowed:
- registry.npmjs.org (port 443)
- npmjs.org (port 443)
- npmjs.com (port 443)
Some firewalls block large downloads - npm packages can be several MB. Ask IT to whitelist npm traffic.
### IPv6 Deep Dive
Node.js 17+ changed the default DNS resolution order to prefer IPv6. This causes issues because:
1. Your router advertises IPv6 via SLAAC
2. Node.js tries to connect via IPv6 first
3. Your ISP doesn't actually route IPv6 properly
4. Connection hangs until timeout
The permanent fix is either:
- Disable IPv6 on your router/system
- Use --dns-result-order=ipv4first flag
- Configure npm with prefer-ip-version 4
To check if IPv6 is the culprit:
# Try forcing IPv4 at the curl level
curl -4 https://registry.npmjs.org
# vs IPv6
curl -6 https://registry.npmjs.orgIf IPv4 works but IPv6 hangs, you've found your problem.
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