Building the Code for Production
Building for production means running your React project through a build tool that turns your source files into a small set of optimised static files: minified JavaScript, CSS and an index.html. Browsers cannot run JSX directly, and the development server is not designed to serve real visitors, so this step always comes before deployment.
What the build step actually does
Your source code is written for people: many small files, JSX, readable variable names and helpful warnings. The production build rewrites it for browsers:
- Transpiling JSX and modern syntax into plain JavaScript the target browsers understand.
- Bundling hundreds of modules into a few files so the browser makes fewer requests.
- Minifying by removing whitespace and comments and shortening names.
- Tree shaking, which drops exported code that nothing imports.
- Code splitting, so lazy-loaded routes from the previous lesson become separate chunks downloaded only when needed.
- Content hashing in file names, such as
index-a1b2c3.js, so browsers can cache files for a long time and still pick up new versions.
React itself also switches to its production mode, which removes development-only checks and warnings and runs noticeably faster.
Running the build
Both common setups use the same script name. The output folder differs: Vite writes to dist/, while Create React App writes to build/.
# install exact dependencies from the lock file
npm ci
# create the optimised production files
npm run build
# Vite only: serve the dist folder locally to test it
npm run preview
# any setup: serve the output folder with a static server
npx serve -s dist
Always test the built version locally before uploading it. Some bugs, such as wrong asset paths, appear only in the production build.
Environment variables are baked in
Values like an API base URL are usually provided through environment variables. In Vite they must start with VITE_ and are read with import.meta.env.VITE_API_URL; in Create React App they start with REACT_APP_ and are read from process.env.
The key fact is that these values are inserted into the JavaScript at build time. Changing the variable on the server afterwards does nothing until you build again. And because the result is shipped to every visitor's browser, anyone can read it. Never put secret keys, database passwords or private tokens in a front-end build.
Common problems after building
- Blank page with 404 errors for JS files. The app is served from a sub-folder but was built for the root. Set
basein Vite's config orhomepageinpackage.jsonfor Create React App. - Refreshing a route gives 404. The server looks for a real file at
/products/5. Single-page apps need the server to sendindex.htmlfor unknown paths; the deployment lessons cover this. - Huge bundle warning. The build tool warns when a chunk is very large. Lazy loading heavy pages or libraries usually fixes it.
- Source maps in production. They help debugging but expose readable source code. Decide deliberately whether to publish them.
What to upload
Only the contents of the output folder go to the host. Your src folder, node_modules and configuration files stay on your machine or in your repository. The output is plain static files, so any static host or web server can serve it.
Before each deployment: install from the lock file, set the right environment variables, run npm run build, preview locally, then upload the output folder.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.