Your local server works at one address but not another: localhost loads but 127.0.0.1 doesn't (or the reverse), or a tool can't connect to localhost at all.
- `http://localhost:5173` works but `http://127.0.0.1:5173` is refused, or vice versa.
- `connect ECONNREFUSED ::1:5432` from Node or another client.
- localhost works in the browser but not from Docker or a VM.
Quick fix
Check which addresses your server actually listens on:
lsof -nP -iTCP:5173 -sTCP:LISTEN
[::1]:5173 means IPv6 loopback only; 127.0.0.1:5173 means IPv4 only; *:5173 means everything. Then either use the matching address, or start the server on both, for example vite --host 127.0.0.1 or --host 0.0.0.0.
Why it happens
| localhost resolves to ::1 first, and the server listens only on 127.0.0.1 (or the reverse). | Most likely |
| The server isn't running, or runs on another port. | Most likely |
| You're inside Docker or a VM, where localhost means the container or VM itself. | Sometimes |
| A proxy setting or /etc/hosts entry changes where localhost goes. | Rarely |
Fixes
Understand what localhost resolves to
- Run
dscacheutil -q host -a name localhost. macOS returns both::1(IPv6) and127.0.0.1(IPv4). - Browsers try both and use whichever answers; many command-line tools and drivers try only the first.
- Example from a Mac running Vite:
curl http://localhost:4322works (via ::1), whilecurl http://127.0.0.1:4322fails, because the server listens on[::1]only.
- Run
Make the server listen where you connect
- Vite:
--host 127.0.0.1, orserver.hostin the config. Nodeserver.listen(port, '::')accepts both IPv4 and IPv6. - Or point clients at the exact address the server uses, such as
127.0.0.1in database URLs.
- Vite:
From Docker to your Mac
- Inside a container, use
host.docker.internalinstead of localhost. - The service on the Mac must listen on more than loopback for some setups; check with lsof.
- Inside a container, use
Expose localhost to the internet (temporarily)
- Use a tunnel such as
cloudflared tunnel --url http://localhost:3000,ngrok http 3000ortailscale funnel 3000. - Stop the tunnel when you're done; it makes your local server public.
- Use a tunnel such as
Still not working?
If nothing listens at all, the server crashed or picked another port; read its startup output. If the browser fails with ERR_CONNECTION_REFUSED on every address, see the connection-refused guide.
See it in the app
LightAtom's Ports view shows whether each local server listens on localhost only, a specific address or all interfaces (hover to see the exact addresses).
Frequently asked questions
What is the difference between localhost and 127.0.0.1?
127.0.0.1 is the IPv4 loopback address. localhost is a name that resolves to loopback addresses, on a Mac both ::1 (IPv6) and 127.0.0.1. A server bound to one of them won't answer on the other.
Why does localhost work but 127.0.0.1 doesn't?
The server listens on IPv6 ::1 only. Browsers reach it through localhost, but 127.0.0.1 is IPv4. Make the server listen on 127.0.0.1 too, or use localhost.
How do I access localhost from my phone?
Run the server on 0.0.0.0 (all interfaces) and open your Mac's LAN address, like http://192.168.1.20:3000, from the phone on the same Wi-Fi. Allow the app in the macOS firewall if asked.
How do I expose localhost to the internet?
Use a tunnel service such as Cloudflare Tunnel, ngrok or Tailscale Funnel. They give your local server a public URL without port forwarding.