Skip to main content
React Introduction
CHAPTER 27 Beginner

React Deployment Basics

Updated: May 13, 2026
15 min read

# 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 with npm 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 run npm 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 build
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
This creates a new folder (usually named `dist` or `build`). If you look inside, you won't see your components! You will just see an `index.html` and some minified gibberish JavaScript files. **This folder contains everything your web server needs.**

### Method A: Deploying to Vercel (Recommended)
Vercel is the company behind Next.js, and they offer the best deployment experience for React developers. It connects directly to your GitHub repository.

1. Create a GitHub repository and push your React code to it.
2. Go to [vercel.com](https://vercel.com/) and sign up with your GitHub account.
3. Click **"Add New Project"**.
4. Import your GitHub repository.
5. Vercel will automatically detect that you are using Vite/React.
6. Click **"Deploy"**.

**The Magic:** Every time you `git push` new code to your repository, Vercel will automatically detect the changes, run `npm run build` on their servers, and update your live website within seconds. This is called Continuous Deployment (CI/CD).

### Method B: Deploying to Netlify (Drag and Drop)
If you don't want to use GitHub, Netlify allows manual deployments.

1. Run `npm run build` on your computer.
2. Go to [netlify.com](https://netlify.com/) and create a free account.
3. Go to your Dashboard and select "Add new site" > "Deploy manually".
4. Drag and drop your `dist` (or `build`) folder into the browser window.
5. Your site is live instantly!

## 5. Output Explanations
Once deployed, Vercel or Netlify will provide you with a live URL (e.g., `my-cool-app.vercel.app`). You can immediately share this link on your resume or portfolio. You can also purchase a custom domain (`mycoolapp.com`) and attach it for free in the settings.

## 6. Important: Client-Side Routing Fix
If your app uses React Router, you will encounter a famous bug after deploying:
If a user goes to `yourapp.com/about` and hits "Refresh", they get a **404 Not Found error**.
**Why?** Because the web server is looking for a physical file named `about.html`, which doesn't exist (remember, it's a Single Page Application with only `index.html`).

**The Fix:**
You must tell the hosting server to redirect all traffic to `index.html` so React Router can handle the URL.
- **Vercel:** Create a file named `vercel.json` in your root directory:
  ```json
  {
    "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
  }
  ```
- **Netlify:** Create a file named `_redirects` inside your `public` folder:
  ```text
  /*    /index.html   200
  ```

## 7. Common Mistakes
- **Deploying the entire project folder instead of the `dist` folder:** If using the drag-and-drop method, deploying the root folder (with `node_modules`) will fail or expose your raw code. Only deploy the build output.
- **Forgetting API Keys (Environment Variables):** If your app uses an API key stored in `.env`, that file is excluded from GitHub. You must manually add those variables in the Vercel/Netlify dashboard settings so the server knows them during the build process.

## 8. Best Practices
- Always set up Git and connect your project to Vercel/Netlify for Continuous Deployment. It saves you from having to drag and drop files every time you fix a typo.
- Run `npm run build` locally before pushing code to ensure there are no hidden compile errors.

## 9. Exercises
1. Run `npm run build` in your local project and inspect the generated `dist` folder.
2. Sign up for a free Vercel account.

## 10. Mini Project: Deploy Portfolio App (Checklist)
Follow this checklist to deploy your final project smoothly:

markdown # 🚀 Deployment Checklist

Pre-Deployment

[ ] Remove any unused packages from package.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 is npm 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.

15. Summary

Deploying a React app is easier today than ever before. By generating a static production build and utilizing modern hosting platforms like Vercel or Netlify, you can put your application on the internet in minutes, complete with auto-updating Continuous Deployment from GitHub.

16. Next Chapter Recommendation

Your app is live, but is the code underneath it clean? Will other developers be able to read it? In Chapter 28: React Best Practices and Clean Code, we will cover the architectural patterns that separate junior developers from senior engineers.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·