Maaz Zindani

Loading the index…

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

Vite or SvelteKit dev server works on localhost but not from another device

ErrorThe page opens on localhost but a phone or another PC on the same network cannot reach it

Short 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.

  • Applies to Vite 6, Vite 7, SvelteKit 2, npm 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -- --host
  2. 2Match what you see, then jump to its fix

    1. 1npm run dev --host behaves exactly as before and the second device still cannot connectThe flag was swallowed by npmFix
    2. 2Other devices get connection refused on the dev port while localhost worksserver.host still has its default of localhostFix
    3. 3You run Vite inside WSL2 with host set, yet only the Windows machine itself can open itWSL2 keeps the server away from your LANFix
    4. 4The dev server rejects requests made by a domain name, while the IP address worksA host name outside the allowed listFix

What does the error look like?

  • The page opens on localhost but a phone or another PC on the same network cannot reach it
  • Connection refused or a timeout on the machine's LAN address and the dev port
  • A 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.json

2Fix

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 dev

Confirmed by docs.npmjs.com

Did this fix it?

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 dev

Confirmed by vite.dev

Did this fix it?

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 -I

2Fix

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>:5173

Confirmed by learn.microsoft.com

Did this fix it?

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:5173

Confirmed by vite.dev

Did this fix it?

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 fix

Next step

Want this handled for good?

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