Professional React WorkflowExtra· 35 min read

Building & Deploying Your React App

Turn your project into a production build and put it live on the internet with a real URL.

What you will learn

  • Create a production build with npm run build
  • Use environment variables for API URLs and keys
  • Deploy to Netlify or Vercel

From dev server to a real website

All through this course you ran npm run dev — a development server on your own machine. To put your app on the internet so anyone can visit it, you first create a production build: an optimised, minified bundle of plain HTML, CSS and JavaScript that browsers can serve fast. Then you upload that build to a host (a service that puts your files on the web and gives you a URL).

Step 1 — build

Create the optimised production build
npm run build

This one command tells Vite to bundle and minify everything into a new dist/ folder. That folder contains your whole site as static files — the thing you actually deploy. You can preview it locally first with npm run preview.

Note: Output: vite v5 building for production… ✓ 34 modules transformed. dist/index.html 0.46 kB dist/assets/index-a1b2c3.js 143.20 kB │ gzip: 46.10 kB ✓ built in 1.20s Everything your site needs now lives in the dist/ folder.

Step 2 — environment variables

Your app often needs values that differ between your computer and the live site — most commonly the API URL (your local server vs the deployed one) and public API keys. You keep these in environment variables so you do not hard-code them. With Vite, put them in a .env file and prefix each with VITE_ so the app can read them.

.env — config that changes per environment
# .env  (do NOT commit secrets to Git)
VITE_API_URL=https://api.myapp.com
Read an env variable with import.meta.env
// read it anywhere in your code:
const apiUrl = import.meta.env.VITE_API_URL;
fetch(apiUrl + '/users').then(/* ... */);

You define VITE_API_URL once in .env, then read it as import.meta.env.VITE_API_URL in your code. On the host’s dashboard you set the same variable to the production value, so the deployed app talks to your real API without changing any code. Only the VITE_ prefix makes a variable readable in the browser — that prefix is a reminder that these values are public, so never put true secrets (like a private database password) here.

Step 3 — deploy

Two beginner-friendly hosts dominate for React: Netlify and Vercel. Both connect to your GitHub repo and redeploy automatically every time you push. The typical flow:

  1. Push your project to a GitHub repository.
  2. On Netlify or Vercel, click New project / Import and pick that repo.
  3. Set the build command to npm run build and the publish directory to dist (these hosts usually detect Vite automatically).
  4. Add your environment variables (e.g. VITE_API_URL) in the host’s settings.
  5. Click Deploy. After a minute you get a live URL like https://my-app.netlify.app.

Note: Output: A public URL such as https://my-app.vercel.app that anyone can open. From now on, every git push to your main branch rebuilds and updates the live site automatically.

Tip: A single-page app needs one extra rule so deep links work: tell the host to send all paths back to index.html (Netlify: a _redirects file with /* /index.html 200; Vercel handles it automatically for Vite). Without it, refreshing on /about can 404.

Watch out: Never commit secret keys to Git. Add .env to your .gitignore, and set production values in the host’s dashboard instead. Anything with the VITE_ prefix is shipped to the browser and is therefore public.

Q. Which command creates the optimised production build, and where does it go?

Answer: npm run build produces an optimised, minified bundle in the dist/ folder — that static folder is what you deploy to a host.

✍️ Practice

  1. Run npm run build on an earlier project and inspect the dist/ folder.
  2. Add a VITE_API_URL env variable and read it with import.meta.env in your fetch.

🏠 Homework

  1. Deploy one of your React projects to Netlify or Vercel and share the live URL. Add a .env for the API URL and set it in the host’s dashboard.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →