How to Host Your React.JS Application to Firebase
Tags: firebase, react, deploy
Published: 2021-04-17 12:36:38

Previously I wrote a blog about my portfolio. If you haven't checked it out yet, check it out here:

{% link https://dev.to/code2rithik/my-new-react-portfolio-is-out-37l7 %}

In the ending of the post, I mentioned that I deployed my React Application to Firebase. 

Want to know how to do this as well?

Well today, I am going to be showing you the same.

# Getting Started

In this tutorial, I will be demonstrating how you can deploy your React app using Firebase Hosting. I will assume that you already have a Firebase project set up. If you do not, you can create one using the Firebase Console.
You will also need to ensure have a React app created. For this, I will use Create React App. You can create a new React app by running the following:

<code>npx create-react-app firebase-hosting-demo</code>

# 1. Installing the Firebase CLI Tools

You will need the Firebase CLI Tools in order to deploy your app to the internet.

You can do this by using the command:

<code>npm install firebase-tools -g</code>

(Don't forget to add the -g flag or else it will not be enabled globally and will set the scope to only **this** project)

# 2. Login to Firebase

Login to Firebase console by using:

<code>firebase login</code>

Then, proceed with your google account to login to the Firebase console.

# 3. Initialize Firebase

The next step is to Initialize Firebase with your project using:

<code>firebase init</code>

You will then be prompted with a series of questions and configuration options that I will walk through with you.

Select Hosting: Configure and deploy Firebase Hosting sites.

Then, create a project on the <a href="https://console.firebase.google.com/">Firebase Console</a>

Then, back in the CLI:

Select Use an existing project

Then, select your firebase project that you created.

You will now need to specify the folder where Firebase will look for assets to deploy. By default, Create React App will generate a build folder that will contain the production assets. Unless you changed the default configuration, you’ll want to enter build for this option.

You will also be asked whether or not Firebase should configure as a single-page app. You’ll want to enter y (Yes) for this option.

The last option is whether or not Firebase should overwrite your existing build/index.html. If you haven’t created a build for your app yet, then you won’t see this option. You’ll want to enter N (No) for this option, though.

### Auto Generated Configuration files

After you have completed the initialization, you should see 2 new files <code>.firebaserc</code>, <code>firebase.json</code>. These files are automatically generated and you will want to save these files, and commit them in your git repo, as they live in your firebase hosting config. You can ignore the .firebase directory for now.

The <code>.firebaserc</code> should look similar to the following:

```json
{
  "projects": {
    "default": "firebase-hosting-demo"
  }
}
```

The <code>firebase.json</code> should look similar to this:

```json
{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
```

# 4. Deploying your App

Now that everything is set up, you can go ahead and deploy your app! All you need to do now is run:

<code>firebase deploy</code>

Firebase will then give you a unique URL where your deployed app is located. For example, it might look similar to:

<code>https://yourprojectid.firebaseapp.com</code>

or

<code>https://yourprojectid.web.app</code>

And thats pretty much it!

Thank You for reading my blog and remember...

# Keep Coding Y'All 👨🏻‍💻