Vite prints a URL but the page will not open
Local: 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.
Fix it fast
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:51732Match what you see, then jump to its fix
- 1
The terminal shows a port other than 5173The port moved because the default was busyFix - 2
Works 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
Running 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
Blocked request. This host is not allowedThe hostname you use is not in allowedHostsFix
- 1
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 :51732Fix
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 viteConfirmed by vite.dev
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 --host2Fix
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>:5173Confirmed by vite.dev
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:51732Fix
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:5173Confirmed by vite.dev
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:5173Confirmed by vite.dev
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb 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 fix