Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Vite prints a URL but the page will not open

ErrorLocal: http://localhost:5173/ (but the browser shows connection refused)

Short answer

Vite started, but the address you are opening is not the one it listens on. Read the URL it prints: the port may have moved because 5173 was busy, the host may be bound only to localhost while you come from another machine or container, or the request host is not in allowedHosts.

  • Applies to Vite 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Start the server and test the exact URL it prints with curl, from the same machine, before using the browser.

    npx vite
    # in a second terminal:
    curl -I http://localhost:5173
  2. 2Match what you see, then jump to its fix

    1. 1The terminal shows a port other than 5173The port moved because the default was busyFix
    2. 2Works at http://localhost:5173 on the machine, fails from a phone, a VM or a Docker hostThe server listens on localhost only, but you connect from elsewhereFix
    3. 3Running inside VS Code Dev Containers or a forwarded port, and localhost on the host does not answerPort forwarding in a dev container does not support IPv6Fix
    4. 4Blocked request. This host is not allowedThe hostname you use is not in allowedHostsFix

What does the error look like?

  • Local: http://localhost:5173/ (but the browser shows connection refused)
  • Blocked request. This host ("myapp.test") is not allowed.

Cause 1 of 4

The port moved because the default was busy

1Check

If the port is taken, Vite quietly tries the next free one, so a bookmark to 5173 now hits another program or nothing.

lsof -i :5173

2Fix

Stop the other process, or make Vite fail instead of moving on so the mismatch is obvious.

// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
  server: { port: 5173, strictPort: true },
})

3Verify

Start Vite again; it either serves on 5173 or exits with an error.

npx vite

Confirmed by vite.dev

Did this fix it?

Cause 2 of 4

The server listens on localhost only, but you connect from elsewhere

1Check

server.host defaults to localhost. Another device on the network, or a container's published port, cannot reach it.

npx vite --host

2Fix

Listen on all addresses. On WSL2, host true alone does not expose the server to your LAN.

// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
  server: { host: '0.0.0.0' },
})

3Verify

Open http://<your-machine-ip>:5173 from the other device.

curl -I http://<your-machine-ip>:5173

Confirmed by vite.dev

Did this fix it?

Cause 3 of 4

Port forwarding in a dev container does not support IPv6

1Check

VS Code forwarding does not handle IPv6, and localhost can resolve to an IPv6 address.

curl -I http://127.0.0.1:5173

2Fix

Set the host to 127.0.0.1 explicitly.

// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
  server: { host: '127.0.0.1' },
})

3Verify

Open http://127.0.0.1:5173 in the host browser.

curl -I http://127.0.0.1:5173

Confirmed by vite.dev

Did this fix it?

Cause 4 of 4

The hostname you use is not in allowedHosts

1Check

localhost, .localhost subdomains and IP addresses are allowed. A custom name or tunnel hostname is not, unless it is listed.

grep -n allowedHosts vite.config.*

2Fix

Add the hostname to the list. Prefer an explicit list over true, because true exposes your source to DNS rebinding.

// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
  server: { allowedHosts: ['myapp.test', '.example.com'] },
})

3Verify

Restart Vite and open the same hostname.

curl -I http://myapp.test:5173

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Use strictPort so a busy port is an error, not a surprise.
  • Keep an explicit host in config when the app runs inside Docker or a dev container.
  • Never set allowedHosts to true on a machine reachable from outside.

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:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.