The React dev server cannot proxy a request to the backend (ECONNREFUSED)
Could not proxy request /pusher/auth from localhost:3000 to http://localhost:5000Short answer
ECONNREFUSED means nothing accepted the connection at the proxy target. The backend may not be running, the port may differ from the proxy setting, or localhost resolves to an IPv6 address the backend does not listen on. Start the backend first, then use one explicit address and port in the proxy setting.
Fix it fast
1Find out which cause you have
Ask the backend directly, without the dev server. If this fails, the proxy is fine and the backend is the problem.
curl -i http://127.0.0.1:5000/2Match what you see, then jump to its fix
- 1
curl to the backend port is refused too.The backend is not running, or runs on another portFix - 2
The backend works on 127.0.0.1 but is refused through localhost.localhost resolves to an address the backend does not listen onFix - 3
The project uses Vite and the error shows a path that the backend does not serve.Vite proxy entry has the wrong target or pathFix
- 1
What does the error look like?
Could not proxy request /pusher/auth from localhost:3000 to http://localhost:5000(ECONNREFUSED)
Cause 1 of 3
The backend is not running, or runs on another port
1Check
Check what the proxy target is, and what is listening.
grep -n "proxy" package.json
lsof -iTCP -sTCP:LISTEN -P | grep node2Fix
Start the backend, and make the port in the proxy setting the one it prints. Restart the dev server after changing package.json.
// package.json (Create React App)
"proxy": "http://localhost:5000"3Verify
Repeat the failing request from the app and read the dev server output.
curl -i http://127.0.0.1:5000/Confirmed by create-react-app.dev
Cause 2 of 3
localhost resolves to an address the backend does not listen on
1Check
Since Node.js 17 the default DNS result order is verbatim, so addresses are not reordered and localhost can resolve to ::1 first. A backend bound to IPv4 only refuses it.
curl -i http://127.0.0.1:5000/
curl -i "http://[::1]:5000/"2Fix
Point the proxy at the explicit IPv4 address, so name resolution cannot pick a different one.
"proxy": "http://127.0.0.1:5000"3Verify
Restart the dev server and repeat the request.
npm startConfirmed by nodejs.org
Cause 3 of 3
Vite proxy entry has the wrong target or path
1Check
In Vite the proxy lives in vite.config. Requests whose path starts with the key go to the target; a rewrite changes the path before forwarding.
grep -n "proxy" -A8 vite.config.*2Fix
Set the target to the backend's real address and use rewrite when the backend does not expect the prefix.
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://127.0.0.1:5000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
},
},
})3Verify
Restart Vite and call the route through the dev server.
curl -i http://localhost:5173/api/healthConfirmed by vite.dev
How do I stop it coming back?
- Put the backend port in one env file and read it in both the server and the proxy setting.
- Use 127.0.0.1 in dev configs so name resolution cannot change the target.
- Start the backend before the dev server in your dev script.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix