How to Deploy React App to Firebase Hosting Easily
To deploy a React app to Firebase, first build your app using
npm run build, then initialize Firebase in your project with firebase init, and finally deploy using firebase deploy. This uploads your app to Firebase Hosting, making it live on the web.Syntax
Here are the main commands to deploy a React app to Firebase Hosting:
npm run build: Creates a production-ready build of your React app in thebuild/folder.firebase init: Sets up Firebase Hosting in your project folder and creates configuration files.firebase deploy: Uploads your build files to Firebase Hosting and makes your app live.
bash
npm run build firebase init firebase deploy
Example
This example shows how to deploy a React app step-by-step:
- Run
npm run buildto create the build folder. - Run
firebase initand select Hosting. Choose thebuildfolder as the public directory and configure as a single-page app. - Run
firebase deployto upload and publish your app.
bash
npm run build firebase init # Select Hosting, set public directory to 'build', configure as SPA firebase deploy
Output
✔ Deploy complete!
Project Console: https://console.firebase.google.com/project/YOUR_PROJECT_ID/overview
Hosting URL: https://YOUR_PROJECT_ID.web.app
Common Pitfalls
Watch out for these common mistakes:
- Not running
npm run buildbefore deploying, which means Firebase uploads old or missing files. - Choosing the wrong public directory during
firebase init. It must bebuildfor React apps. - Forgetting to configure as a single-page app (SPA) during
firebase init, causing routing issues. - Not logging into Firebase CLI with
firebase loginbefore deploying.
bash
Wrong: firebase init # Set public directory to 'public' (wrong) firebase deploy Right: firebase init # Set public directory to 'build' (correct) firebase deploy
Quick Reference
Summary tips for deploying React apps to Firebase:
- Always run
npm run buildbefore deploying. - Set the public directory to
buildduringfirebase init. - Enable single-page app rewrite to support React routing.
- Use
firebase loginto authenticate before deploying. - Check your Firebase project ID matches your Firebase console.
Key Takeaways
Run 'npm run build' to prepare your React app for deployment.
Use 'firebase init' to configure Firebase Hosting with 'build' as the public folder.
Enable single-page app support during initialization to handle React routes.
Deploy your app with 'firebase deploy' after setup and build.
Always log in with 'firebase login' before deploying.