Vite or SvelteKit dev server works on localhost but not from another device
The page opens on localhost but a phone or another PC on the same network cannot reach itShort answer
Vite listens on localhost only unless you change server.host. The flag also has to reach Vite: with npm run you need -- before it. Set host in the config or run npm run dev -- --host, then open the machine's LAN address with the dev port. Under WSL2, host alone is not enough.
Fix it fast
1Find out which cause you have
Start the server with the host option passed after a double dash, then open it from a second device by the machine's LAN address and the port Vite prints.
npm run dev -- --host2Match what you see, then jump to its fix
- 1
npm run dev --host behaves exactly as before and the second device still cannot connectThe flag was swallowed by npmFix - 2
Other devices get connection refused on the dev port while localhost worksserver.host still has its default of localhostFix - 3
You run Vite inside WSL2 with host set, yet only the Windows machine itself can open itWSL2 keeps the server away from your LANFix - 4
The dev server rejects requests made by a domain name, while the IP address worksA host name outside the allowed listFix
- 1
What does the error look like?
The page opens on localhost but a phone or another PC on the same network cannot reach itConnection refused or a timeout on the machine's LAN address and the dev portA domain name pointing at the dev machine is rejected by the Vite dev server
Cause 1 of 4
The flag was swallowed by npm
1Check
Flags that start with a dash are read by npm itself unless a double dash comes first. Everything after the double dash goes to the script.
grep -n '"dev"' package.json2Fix
Put -- between the script name and the flag, or write the flag into the script so nobody forgets it.
npm run dev -- --host
# package.json
"scripts": { "dev": "vite dev --host" }3Verify
The second device can open the LAN address and the port.
npm run devConfirmed by docs.npmjs.com
Cause 2 of 4
server.host still has its default of localhost
1Check
The default is localhost. Setting 0.0.0.0 or true makes Vite listen on all addresses, LAN and public ones included. On the CLI, --host alone or --host 0.0.0.0 does the same.
grep -rn "host" vite.config.*2Fix
Set it in the config file so it holds for every start, including SvelteKit, which reads the same Vite option.
// vite.config.js
export default {
server: { host: true },
};3Verify
Open the machine's LAN address and port on a phone on the same Wi-Fi.
npm run devConfirmed by vite.dev
Cause 3 of 4
WSL2 keeps the server away from your LAN
1Check
Vite's docs say that on WSL2 setting host to true is not enough for LAN access. In the default NAT mode WSL2 has its own virtual adapter with its own IP address, so the LAN cannot see it.
wsl.exe hostname -I2Fix
In PowerShell on Windows, add a port proxy from the Windows address to the WSL2 address, or on Windows 11 22H2 and newer set networkingMode=mirrored under [wsl2] in .wslconfig. Allow the inbound connection in the firewall as the Microsoft page describes.
netsh interface portproxy add v4tov4 listenport=5173 listenaddress=0.0.0.0 connectport=5173 connectaddress=<wsl-ip-from-check>3Verify
A second device opens the Windows machine's address with the dev port.
curl -I http://<windows-lan-ip>:5173Confirmed by learn.microsoft.com
Cause 4 of 4
A host name outside the allowed list
1Check
localhost, .localhost subdomains and all IP addresses are allowed by default. Any other domain name pointing to the dev machine is blocked until it is listed.
grep -rn "allowedHosts" vite.config.*2Fix
Add the name to server.allowedHosts. Use an explicit list; the docs advise against allowing every host.
// vite.config.js
export default {
server: { host: true, allowedHosts: ['my-site.example.com'] },
};3Verify
Open the site by that name.
curl -I http://my-site.example.com:5173Confirmed by vite.dev
How do I stop it coming back?
- Put host in vite.config so the setting does not depend on how someone typed the command.
- Keep strictPort set to true if other devices use a saved address; otherwise Vite moves to the next port when 5173 is busy.
- Expose the dev server only on networks you trust; it is not a production server.
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