Sub topic 13
Running the dev server and hot updates
This chapter assumes you have already scaffolded a project with npm create vite@latest, run npm install, and have Node.js and npm working. Here you start the dev server, see what it does, and learn how hot updates behave, including when they fail.
Starting the server
From the project folder, run the dev script defined in package.json:
npm run devVite prints a local address, by default http://localhost:5173. Open it in a browser to see the starter app. If port 5173 is taken, Vite picks the next free port and prints it. Stop the server with Ctrl+C.
What the dev server does
The dev server does not bundle your whole app before it starts. The browser requests index.html, which loads src/main.jsx as an ES module. Vite then transforms each file on demand, turning JSX into plain JavaScript, and serves it. Because of this, startup stays fast even as the project grows.
Mermaid diagrams must use the required fence, so here is the flow as a diagram block:
Hot module replacement
Hot module replacement (HMR) is a feature that swaps a changed module in the running page without a full reload. With @vitejs/plugin-react, this is called fast refresh. When you save a component file, React re-renders the edited component and keeps the state of the rest of the page.
Try it with the app from the earlier chapters. Click the counter a few times, then change the heading in src/App.jsx:
function App() {
return (
<main>
<h1>Hot updates at work</h1>
<Counter label="Clicks" />
</main>
)
}After you save, the heading changes at once and the counter keeps its value. If you edit the Counter component itself, React usually keeps its state too, as long as the hooks stay in the same order.
When state resets or the page reloads
Fast refresh does not always preserve state. Know the common cases:
| Situation | What happens |
|---|---|
| You edit the markup or logic of a component | Component re-renders, state kept |
| You change the hooks a component calls | State of that component resets |
| A file exports something other than components, such as constants | Importing modules update, state may reset |
| You edit `index.html` or `vite.config.js` | Full reload, or a server restart for the config |
| A syntax error occurs | Error overlay appears, the page recovers when you fix the file |
Common errors
- Port already in use. Another process holds 5173. Vite moves to another port, so read the printed address instead of assuming.
- Blank page after a typo. Check the browser overlay and the terminal. Both show the file and line.
- Changes not appearing. Confirm you saved the file and that you are editing the project the server is running from.
- Needing the server on your network. Run
npm run dev -- --hostto expose it to other devices.
Exercise
You click the counter to 5, then edit the text inside the <h1> in App.jsx and save. What do you expect to see, and which feature causes it?
Show answer
The heading updates immediately and the counter still shows 5. Hot module replacement (fast refresh) swaps the changed module without reloading the page, so the state of the rest of the app is preserved.