🚀 Build React App for Production
When your React app is ready to go live, you need to optimize and build it for production. React provides a built-in way to do this using npm run build.
🧱 1. Run Build Command
npm run build
This creates an optimized production build in the /build folder.
📦 2. Output Structure
The build folder contains:
index.html– entry pointstatic/js– minified JS bundlesstatic/css– stylesheetsassets– images and other resources
🌐 3. Test Locally
Use a static server to preview your build locally:
npm install -g serve
serve -s build
🚚 4. Deploy Options
- GitHub Pages – For personal projects
- Vercel / Netlify – One-click deployment with Git integration
- Firebase / Render / Railway – Great for full-stack deployment
- Custom Hosting – Upload the
buildfolder to any static hosting provider (Apache, Nginx, cPanel)
🔒 5. Set Environment Variables
In production, React reads variables from .env files starting with REACT_APP_.
REACT_APP_API_URL=https://api.example.com
📦 6. Extra Optimization (Optional)
- Use
React.lazyandSuspensefor lazy loading components - Remove unused dependencies and console logs
- Analyze your bundle using
source-map-explorer