Sub topic 15
Building and Previewing for Production
This chapter assumes you have a Vite React project that runs with npm run dev, and that you know what package.json scripts are. It covers what the build produces, how to check it locally, and the errors people commonly hit.
Why the dev server is not enough
The dev server serves your source files as separate modules and transforms them on request. That is fast to work with, but it is not what you ship. A production build bundles your code, removes unused parts, minifies it, and gives files hashed names so browsers can cache them safely.
Run the build
The scaffolded package.json includes the scripts you need. Run them from the project root.
npm run build
npm run previewnpm run build writes an optimized bundle to the dist folder. npm run preview serves that folder locally so you can check it before deploying. The preview command prints a local address, usually http://localhost:4173.
What ends up in dist
A typical build gives you a layout like this:
index.html, with script and style tags that point to the built files.assets/, holding JavaScript and CSS files with a hash in the name, such asindex-a1b2c3d4.js.- Any files from your
publicfolder, copied as they are.
The hash changes when the file contents change. A host can therefore cache the files for a long time, and visitors still get the new version after you deploy.
The following diagram shows the flow from source to a running preview.
Preview is not a production server
vite preview exists only to check the build on your machine. It is not designed to serve real traffic. For deployment, upload the contents of dist to a static host or put them behind a web server such as Nginx.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Blank page after deploy to a sub-path | Assets load from `/assets/...` but the app lives under `/my-app/` | Set `base: '/my-app/'` in `vite.config.js` |
| Works in dev, fails in build | Import path has the wrong letter case, or a file is missing | Match file name case exactly. Linux hosts are case sensitive |
| `dist` not found when previewing | You did not run the build first | Run `npm run build`, then `npm run preview` |
| Old version still showing | Browser or host cached `index.html` | Hard refresh. Configure the host to not cache `index.html` |
Check the result
After building, look at the output the build prints. It lists each file and its size. If a single JavaScript file is very large, you may be importing a heavy library you do not need. Fix that before you deploy, not after.
Exercise
You run npm run preview in a fresh clone of a project and get an error that dist does not exist. What went wrong, and what do you run?
Show answer
The preview command only serves an existing build, and no build has been made yet. Run npm run build first to create dist, then run npm run preview.
Exercise
Your app works at http://localhost:4173, but after deploying to https://example.com/my-app/ it shows a blank page. Which setting in vite.config.js is the first thing to check?
Show answer
The base option. Set it to '/my-app/' so the built HTML points to assets under that sub-path instead of the site root.