Axios Network Error in Node.js— Fixed [Complete Guide 2026]
⚡ Debug First — Network Error Has Multiple Causes
axios.get('/api/data').catch(error => {
if (error.response) {
// Server responded with error status (4xx, 5xx)
console.log('Status:', error.response.status)
console.log('Data:', error.response.data)
} else if (error.request) {
// Request sent but no response received
console.log('No response:', error.request)
console.log('Code:', error.code) // ECONNREFUSED, ETIMEDOUT etc.
} else {
console.log('Request setup error:', error.message)
}
})What Causes Axios Network Error?
AxiosError: Network Error
at XMLHttpRequest.handleError (axios/lib/adapters/xhr.js:117:14)
# or in Node.js:
AxiosError: connect ECONNREFUSED 127.0.0.1:5000Axios throws 'Network Error' when no response was received — the request never completed. This is different from a 4xx or 5xx error (where the server responded with an error). Network Error means something prevented the response from arriving at all.
| Scenario | Root Cause | Fix |
|---|---|---|
| Browser: works in Postman not browser | CORS — browser blocking response | Jump → |
| Node.js: ECONNREFUSED | Server not running on that port | Jump → |
| Request hangs then fails | Timeout — server too slow | Jump → |
| SSL/TLS error | Certificate invalid or self-signed | Jump → |
| Wrong data in response | Wrong base URL or endpoint | Jump → |
Diagnose With error.response and error.request
Identify the exact cause firstconst handleAxiosError = (error) => {
if (axios.isAxiosError(error)) {
if (error.response) {
// Server responded with non-2xx status
console.error('Server error:', error.response.status, error.response.data)
} else if (error.request) {
// Request was made but no response
console.error('Network error - no response received')
console.error('Error code:', error.code)
// ECONNREFUSED = server not running
// ETIMEDOUT = server too slow
// ERR_NETWORK = CORS or DNS failure (browser)
} else {
// Request setup error
console.error('Request error:', error.message)
}
}
}
// Usage
try {
const res = await axios.get('/api/data')
} catch (error) {
handleAxiosError(error)
}CORS Error in Browser Context
Works in Node.js but fails in browserIn a browser, Axios Network Error is almost always a CORS issue. The request reaches the server but the browser blocks the response because the server is missing Access-Control-Allow-Origin.
const cors = require('cors')
app.use(cors({ origin: 'http://localhost:3000', credentials: true }))Check the browser Network tab in DevTools. If the request shows a red CORS error, the fix is on the server — not in Axios.
Wrong Base URL or Endpoint
404 or no response// Log the full URL Axios is requesting
axios.interceptors.request.use(config => {
console.log('Requesting:', config.baseURL + config.url)
return config
})
// Common mistakes
axios.get('localhost:5000/api/data') // ❌ missing http://
axios.get('http://localhost:5000api/data') // ❌ missing /
axios.get('http://localhost:3000/api/data') // ❌ wrong port
// Correct
axios.get('http://localhost:5000/api/data') // ✅
// Best practice — set baseURL once
const api = axios.create({
baseURL: process.env.REACT_APP_API_URL || 'http://localhost:5000',
timeout: 10000,
})Server Not Running (ECONNREFUSED)
Node.js: connect ECONNREFUSEDECONNREFUSED means nothing is listening on that port. Start your backend server and verify the port.
# Verify server is running and on correct port
curl http://localhost:5000/api/health
# If this fails — server is not running
# Check what is on the port
lsof -i :5000 # macOS/Linux
netstat -ano | findstr :5000 # Windows
# Start your server
npm start
# or
node server.jsSSL Certificate Errors
DEPTH_ZERO_SELF_SIGNED_CERT / unable to verifyconst https = require('https')
const api = axios.create({
// Development only — NEVER in production
httpsAgent: new https.Agent({ rejectUnauthorized: false })
})
// Production — use proper certificate
// or add your CA cert:
const httpsAgent = new https.Agent({
ca: require('fs').readFileSync('/path/to/ca-cert.pem')
})
const api = axios.create({ httpsAgent })Never use rejectUnauthorized: false in production. It disables certificate validation completely and makes HTTPS meaningless.
Timeout Configuration
Request hangs — ETIMEDOUT or ECONNABORTED// Set timeout on axios instance (milliseconds)
const api = axios.create({
baseURL: 'http://localhost:5000',
timeout: 10000, // 10 seconds
})
// Or per request
axios.get('/api/slow-endpoint', { timeout: 30000 })
// Handle timeout specifically
axios.get('/api/data').catch(error => {
if (error.code === 'ECONNABORTED') {
console.error('Request timed out')
}
})Proxy Configuration
Corporate network or custom proxy// Set HTTP proxy
const api = axios.create({
proxy: {
protocol: 'http',
host: 'proxy.company.com',
port: 8080,
auth: { username: 'user', password: 'pass' }
}
})
// Disable proxy for specific requests
axios.get('/api/data', { proxy: false })
// Use environment variables (Axios reads these automatically)
// HTTP_PROXY=http://proxy.company.com:8080
// NO_PROXY=localhost,127.0.0.1Proper Axios Error Handling
Production-ready error handling patternimport axios from 'axios'
const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5000',
timeout: 15000,
withCredentials: true,
})
// Request interceptor — add auth token
api.interceptors.request.use(
config => {
const token = localStorage.getItem('token')
if (token) config.headers.Authorization = `Bearer ${token}`
return config
},
error => Promise.reject(error)
)
// Response interceptor — handle errors globally
api.interceptors.response.use(
response => response,
error => {
if (error.response?.status === 401) {
// Redirect to login
window.location.href = '/login'
}
if (error.code === 'ERR_NETWORK') {
console.error('Network error — check server and CORS')
}
return Promise.reject(error)
}
)
export default apiHow to Prevent Axios Network Errors
- Set a baseURL on your Axios instance — avoid hardcoded URLs scattered across files
- Always add CORS middleware to your backend before routes
- Set a reasonable timeout — never let requests hang forever
- Use Axios interceptors for global error handling
- Verify server is running before testing frontend
- Use environment variables for API URLs — REACT_APP_API_URL or NEXT_PUBLIC_API_URL
Frequently Asked Questions
What causes Axios Network Error?+−
Axios Network Error with no response — how to debug?+−
Axios Network Error only in browser not in Node?+−
How do I fix Axios ECONNREFUSED?+−
How do I handle Axios errors properly?+−
Axios SSL certificate error — how to fix?+−
Need Expert Help?
We Build Production-Grade Node.js Apps
Softplix builds full-stack applications. Dealing with API integration issues in production? We can help.
Talk to an Engineer