Build and Environment
โš›๏ธ React 18+ ๐ŸŸข Chapter 38 of 39 ๐Ÿ“‚ Phase 17: Production and Deployment ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: Development vs Production ยท Production Build ยท Environment Variables ยท API Base URLs ยท Build Optimization ยท Source Maps ยท Bundle Analysis
Everything so far has run in Vite's development server. This chapter covers what changes when you prepare a React app to actually ship to real users.
1Development vs Production Builds
npm run dev     # development server - fast refresh, unminified, helpful error overlays
npm run build   # production build - minified, optimized, outputs to dist/

The development server (used throughout this entire course) prioritizes fast feedback and helpful debugging tools. A production build strips out development-only warnings, minifies your code to a fraction of its original size, and optimizes it for real users' actual load times โ€” the two modes are deliberately very different.

2Environment Variables
๐Ÿ’ป Example 1: Configuring an API Base URL
Terminal / JSX
# .env.development
VITE_API_URL=http://localhost:3001

# .env.production
VITE_API_URL=https://api.myapp.com
// In your code:
const API_URL = import.meta.env.VITE_API_URL;

fetch(`${API_URL}/users`);
๐Ÿ” Why This Matters:

Chapter 21's fetch("/api/users") calls need to point somewhere different in development versus production. Environment variables (note the required VITE_ prefix in Vite projects) let the same code automatically use the right URL in each environment, without hardcoding it or manually editing files before each deploy.

3Build Optimization and Bundle Analysis

Vite automatically handles significant optimization during npm run build: minification (removing whitespace and shortening variable names), tree-shaking (removing code you imported but never actually use), and code-splitting your lazy()-loaded chunks from Chapter 31 into separate files. To see exactly what's taking up space in your final bundle:

npm install --save-dev rollup-plugin-visualizer

Tools like this generate a visual treemap of your final bundle, making it easy to spot an accidentally included huge library or duplicate dependency that's bloating your app's download size.

4Source Maps for Debugging Production Errors

Since production code is minified into unreadable single-letter variable names, source maps are separate files that map that minified code back to your original, readable source โ€” letting error-tracking tools (and your own browser DevTools, if enabled) show you the real file and line number when a production error occurs, rather than an unreadable minified stack trace.

โš ๏ธ Hardcoding API URLs Directly in Component Code

Writing fetch("http://localhost:3001/users") directly works fine in development, but breaks completely the moment you deploy to production, where that localhost address doesn't exist. Always use environment variables for anything that differs between environments โ€” API URLs being the most common example.

๐Ÿ’ป Hands-on Interactive Practice Challenge

Set up a .env file with a variable for an API base URL, and reference it in a fetch call using import.meta.env.

React Practice Challenge โ–ถ Run in Compiler
// .env
VITE_API_URL=https://jsonplaceholder.typicode.com

// App.jsx
function App() {
  const API_URL = import.meta.env.VITE_API_URL;

  useEffect(() => {
    fetch(`${API_URL}/users`)
      .then(res => res.json())
      .then(console.log);
  }, []);

  return <p>Check the console for fetched data.</p>;
}
Run This Challenge in Online JS IDE โ†’
โ“ Frequently Asked Questions (FAQ)

Q Why do Vite environment variables need a VITE_ prefix?

This is a deliberate security measure โ€” Vite only exposes environment variables prefixed with VITE_ to your client-side code, preventing you from accidentally bundling sensitive server-only secrets (like database passwords) into code that ships to every visitor's browser.

Q Should .env files be committed to Git?

Generally, no โ€” .env files often contain values that differ per developer or environment (and sometimes secrets), so they're typically added to .gitignore. A .env.example file with placeholder values is usually committed instead, documenting what variables are needed.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on React 18+ ยท Last updated August 2026