Hướng dẫn Deploy an Angular App on Firebase năm 2021
5th Sep 2021Create a Firebase Account
In order to deploy your app on Firebase, you must first need to have an account on the firebase website ( https://console.firebase.google.com).
The good news is that Firebase expects Google account for login, and I am sure you already have one. Otherwise, you can create one if you don’t have or you can create another Google account for Firebase if you don’t want to use an existing one.
Create a New Firebase Project
Once you are done with the account creation, now create your first Firebase project by clicking on the Add Project button. It’s a three-step process.
First, provide a unique name for your project. In my case, I have named it angular7-todo-app.
Second, enable Google analytics for this project, which is by default enabled so click on continue. Finally, configure the google analytics by selecting default account for firebase from the dropdown field and click on create project.
It will take 1-2 minutes to create the project, and that’s all you need to do on Firebase site for now in order to deploy this Todo App. Although you can do much more with Firebase, that’s not in scope for this article.
Creating Product app in Firebase
Deploy App With Firebase Tool
Now we have all the setup needed at the Firebase site done, we are few steps away from the deployment of our app on Firebase.
Install Firebase Tools
We will require firebase tools installed on the machine for the deployment.
So open the command or bash window and navigate to your app project folder(in my case todo-app) and install firebase-tools on your machine via npm by running the following command:
npm install –g firebase-tools
Installing Firebase tools
Next, we need to login to Firebase from the command prompt/bash window by running following command:
firebase login --interactive
This will open the Firebase login in the browser for you to login so that we can work on our Firebase project from our machine using firebase tools CLI.
Configure Firebase Project Using Firebase-Tools
Now, in order to configure our Firebase project, we need to initialize the process in the project folder using following command:
firebase init
This will start a selection wizard with following steps in the console.
Step 1 —Select “Y” to proceed and then select the "Hosting: Configure and deploy Firebase Hosting sites" option since we want to only host our app at this moment.
Proceeding with "Y"
Hosting: Configure and deploy Firebase Hosting sites
Step 2 — Now, select "Use an existing project" option since we already created a Firebase project for this application.
Use an existing project
Step 3 — Next, select the newly created Firebase project, which, in my case is “demofirebase.”
Selecting existing project
Step 4 – Type “No” to the "Configure as a single-page app" option since we will configure it by updating firebase.json file later on.
Not configuring application as SPA
The Firebase initialization wizard will create the following files in the project folder:
- 404.html.
- index.html.
- .firebaserc.
- Firebase.json.
Delete this index.html file since you might already have an index.html in your app project folder.
Now, edit this newly created firebase.json file and add the following configuration.
{ "hosting": { "public": "docs", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "**", "destination": "/index.html" } ] } }
Note: here public key value refers to the deployment folder for the App so make sure the outputPath value in the angular.json should be docs.
This setting will redirect the request to index.html page whenever the App is refreshed.
Checking outputPath value
Build the Angular App
Now, run the following commad on the command/bash window from your app project folder to generate distributable files in the docs folder:
ng build --prod
Building applciation
Deploy the App on Firebase
Finally, deploy the app by running following command on the command/bash window from your app project folder:
firebase deploy
Deploying the app to Firebase
Now, open the hosting URL in the browser, which in this case
Add new comment