How to Deploy React App to Vercel Quickly and Easily
To deploy a React app to
Vercel, first push your React project to a GitHub repository. Then, connect that repository to Vercel via its dashboard, and Vercel will automatically build and deploy your app with zero configuration.Syntax
Deploying a React app to Vercel involves these main steps:
- Push code to GitHub: Your React app source code must be in a GitHub repository.
- Connect repository to Vercel: Use the Vercel dashboard to import your GitHub repo.
- Configure build settings: Vercel auto-detects React apps and sets
npm run buildas the build command andbuildas the output directory. - Deploy: Vercel builds and deploys your app, providing a live URL.
bash
npx create-react-app my-app cd my-app git init git add . git commit -m "Initial commit" git branch -M main git remote add origin https://github.com/yourusername/your-repo.git git push -u origin main
Example
This example shows how to deploy a simple React app to Vercel:
- Create a React app using
create-react-app. - Push it to GitHub.
- Go to Vercel dashboard, click New Project, and import your GitHub repo.
- Vercel detects React and sets build commands automatically.
- Click Deploy and wait for the live URL.
bash
npx create-react-app hello-vercel cd hello-vercel git init git add . git commit -m "Deploy React app" git branch -M main git remote add origin https://github.com/yourusername/hello-vercel.git git push -u origin main
Common Pitfalls
Here are common mistakes when deploying React apps to Vercel:
- Not pushing code to GitHub: Vercel requires a Git repository to connect.
- Wrong build output folder: React apps built with
create-react-appoutput tobuild, notdist. - Missing
homepageinpackage.json: This can cause routing issues if your app is not at root URL. - Not committing build files: You do NOT commit build files; Vercel builds on its servers.
Wrong example:
vercel --prod
This command is for Vercel CLI but requires setup; better to use GitHub integration for beginners.
Quick Reference
Summary tips for deploying React apps to Vercel:
- Push your React app code to GitHub.
- Use Vercel dashboard to import and deploy.
- Ensure
npm run buildworks locally. - Do not commit
buildfolder; Vercel builds it. - Use
homepageinpackage.jsonif deploying to a subpath.
Key Takeaways
Push your React app code to a GitHub repository before deploying to Vercel.
Connect your GitHub repo to Vercel via its dashboard for automatic build and deployment.
Vercel auto-detects React apps and uses 'npm run build' and 'build' folder by default.
Do not commit build files; Vercel builds your app on its servers.
Use the 'homepage' field in package.json if your app is served from a subpath.