Updated:

Vite Localhost Ports 5173 and 4173: Dev, Preview and Fixes

Start Vite on the right port, preview a production build, and diagnose connection refused, occupied ports, API proxy errors, or phone access problems.


Vite normally uses http://localhost:5173 for development and http://localhost:4173 for build preview. These commands serve different things: development works with source files and HMR; preview serves the output of your build. Always use the actual URL printed in the terminal.

Which command should you run?

GoalCommand in a Vite projectDefault address
Edit the app with hot updatesnpm run devhttp://localhost:5173
Generate production filesnpm run buildWrites files, does not start a server
Check the generated build locallynpm run preview after buildinghttp://localhost:4173

These npm commands assume scripts for vite, vite build, and vite preview in package.json. Frameworks built on Vite can have different commands or ports; Astro, for example, uses 4321 for development. vite preview is a local preview server, not a production hosting service. See Vite deployment documentation.

Start a small project

With a supported Node version from the Vite getting started guide, run:

npm create vite@latest localhost-demo -- --template vanilla
cd localhost-demo
npm install
npm run dev -- --port 5173 --strictPort

Keep this terminal running while opening http://localhost:5173. --strictPort makes an occupied port fail explicitly rather than silently selecting another one. Without strict mode, Vite can try a different port: check the terminal instead of assuming it stayed on 5173.

Preview the built app on 4173

In the project directory:

npm run build
npm run preview -- --port 4173 --strictPort

Open http://localhost:4173 only after the preview server reports it is ready. After changing source files, rebuild to update the preview. A running development server does not automatically rebuild the files served by preview.

Development and preview ports have separate configuration:

import { defineConfig } from 'vite';

export default defineConfig({
  server: { port: 5173, strictPort: true },
  preview: { port: 4173, strictPort: true },
});

See server options and preview options.

Troubleshoot in this order

SymptomCheckFix
Browser says connection refusedIs the terminal process still running? What URL did it print?Start the intended server and open its reported address
Port already in useWhich process is listening?Inspect it with the port conflict guide, or choose another port
Preview says build directory is missingHas the build succeeded in this project?Run npm run build, inspect its errors, then restart preview
Preview still shows old contentWere files rebuilt after editing?Rebuild and reload the preview
HTTP 404Requested path, base, and application routingA 404 means an HTTP server answered; check the route rather than treating it as refusal
API call fails but the app opensBackend response and the requested API URLInspect the Network panel; a working frontend does not prove the backend is running

Call a local backend through a development proxy

For a backend listening on port 8080, add this inside the server configuration:

proxy: {
  '/api': {
    target: 'http://127.0.0.1:8080',
    changeOrigin: true,
  },
}

Call fetch('/api/health') from the app if your backend actually implements /api/health. This example forwards that path unchanged. Restart Vite after changing its config. Development proxy settings do not configure your deployed production server.

Open Vite from a phone or another computer

On a trusted local network, run npm run dev -- --host 0.0.0.0. On the phone, open the development computer’s LAN IP and the reported port, such as http://192.168.1.20:5173. The phone’s localhost points to the phone itself. Check the host firewall, Wi-Fi isolation, and WSL networking if the LAN URL fails.

For HMR behind a reverse proxy or file watching under WSL, use the Vite troubleshooting documentation and check WebSocket traffic and watcher behavior before enabling polling.

Related: port 5173, port 4173, React + Vite, and the URL parser.

Local development guides & tools © LocalHost Run