React Deployment Basics
# React Deployment Basics
1. Introduction
You've spent weeks building a beautiful React application, and now you want to share it with a client, a recruiter, or the world. But you can't just send them your folder withnpm run dev. You need to bundle your code into a production-ready format and host it on a web server. In this chapter, we will learn how to build and deploy a React application for free using modern platforms like Vercel and Netlify.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the difference between development code and production code.
- Generate a static production build of your React app.
- Deploy a React application continuously using GitHub and Vercel.
- Deploy an app manually using Netlify.
3. Beginner-Friendly Explanations
Development vs. Production
When you runnpm run dev (Vite), React includes hundreds of helpful warnings, debugging tools, and uncompressed files. This is great for you, but it makes the app heavy and slow for users.
Before deploying, we run a Build Step. The bundler takes all your JSX, components, CSS, and NPM packages, strips out the warnings, compresses the files, and creates standard, highly-optimized HTML, CSS, and JS files. This is called the "production bundle."
Hosting an SPA
Because a compiled React app is just static files (HTML/CSS/JS), you don't need a heavy Node.js or PHP server to host the frontend. You can host it on any static file server, which is why services like Vercel, Netlify, and GitHub Pages offer it for free!4. Step-by-Step Deployment Guide
Step 1: The Build Command
To see what your production app looks like locally, run this in your terminal: ```bash id="ch27_ex1" # Example bash command npm run buildmarkdown # 🚀 Deployment Checklist
Pre-Deployment
[ ] Remove any unused packages frompackage.json to keep bundle small.
[ ] Check console for warnings or errors in the browser.
[ ] Run npm run build locally to verify success.
[ ] Ensure .env variables are secure (no secret database passwords in frontend code!).
Git Setup
[ ]git init
[ ] Create .gitignore (make sure node_modules and .env are in it).
[ ] git add .
[ ] git commit -m "Initial production commit"
[ ] Push to GitHub.
Platform Configuration (Vercel)
[ ] Import repository. [ ] Verify Build Command isnpm run build.
[ ] Verify Output Directory is dist (for Vite) or build (for CRA).
[ ] Add Environment Variables in the UI settings.
[ ] Add routing redirect config (vercel.json if using React Router).
Post-Deployment
[ ] Visit live URL. [ ] Test routing by refreshing a sub-page (e.g./contact).
[ ] Test API calls.
[ ] Change the default Vercel/Netlify URL to something professional.
``
11. Coding Challenges
Challenge 1: Create a simple "Coming Soon" React app, push it to GitHub, and deploy it to Vercel. Share the link with a friend!
12. MCQs with Answers
Q1: What does the npm run build command do in a modern React application?
A) It starts a fast development server.
B) It converts, minifies, and bundles your React code into static production files.
C) It deploys the app to the internet.
*Answer: B*
Q2: Why do React Router apps often show a 404 error when refreshing a sub-route in production?
A) Because React Router is disabled in production.
B) Because the server looks for a physical HTML file that matches the route, but SPAs only have an
index.html.
C) Because the API failed to load.
*Answer: B*
13. Interview Questions
-
Q: Explain the difference between Development mode and Production mode in React.
-
Q: How do you handle environment variables (like API keys) when deploying a React app? *(Answer: You inject them through the hosting provider's dashboard settings during the CI/CD build step).*
14. FAQs
Can I deploy a full-stack app (React + Node.js) to Vercel?
Yes. While Vercel is famous for static frontend hosting, it also supports Serverless Functions, meaning you can write backend Node.js APIs in an /api` folder and Vercel will host them seamlessly alongside your React app.