LightAtom

localhost Not Working on Mac (127.0.0.1 vs ::1)

On a Mac, localhost means both 127.0.0.1 and ::1. Servers that listen on only one of them work with one address and fail with the other.

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

  1. Understand what localhost resolves to

    1. Run dscacheutil -q host -a name localhost. macOS returns both ::1 (IPv6) and 127.0.0.1 (IPv4).
    2. Browsers try both and use whichever answers; many command-line tools and drivers try only the first.
    3. Example from a Mac running Vite: curl http://localhost:4322 works (via ::1), while curl http://127.0.0.1:4322 fails, because the server listens on [::1] only.
  2. Make the server listen where you connect

    1. Vite: --host 127.0.0.1, or server.host in the config. Node server.listen(port, '::') accepts both IPv4 and IPv6.
    2. Or point clients at the exact address the server uses, such as 127.0.0.1 in database URLs.
  3. From Docker to your Mac

    1. Inside a container, use host.docker.internal instead of localhost.
    2. The service on the Mac must listen on more than loopback for some setups; check with lsof.
  4. Expose localhost to the internet (temporarily)

    1. Use a tunnel such as cloudflared tunnel --url http://localhost:3000, ngrok http 3000 or tailscale funnel 3000.
    2. Stop the tunnel when you're done; it makes your local server public.

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.

LightAtom team

The people who build LightAtom, a network monitor for macOS.