Angular CLI is a command-line interface for Angular and one of the easiest ways to get your app started. The beauty of using Angular CLI is that it lets you focus on your code, without having to worry about the structure of your application, since all the necessary files are generated for you.
It is very easy to create production-ready applications with Angular CLI. On the other hand, Firebase makes it fast to host applications. In addition, Firebase has a lot of features and a free plan that lets you experiment with the platform without being tied to a paid plan.
The free plan has the following features:
- A/B testing
- app indexing
- cloud messaging
- crash analytics
- performance monitoring
In order to run Angular CLI, you must have Node.js 6.9 and NPM 3 or higher installed on your system. If you don’t have Node.js installed, please visit the Node.js website to find instructions on how to install Node.js on your operating system.
You should also have a basic understanding of the following:
- object-oriented programming
Installing Angular CLI
Installing Angular CLI is as easy as:
The above command installs the latest version of Angular. To validate the successful installation of Angular CLI, simply issue the following command:
Creating an Angular Application
Now that you have Angular CLI installed, we can start developing our application. In this tutorial, we will not dive into the components that make up an Angular CLI project since this post is mostly about deploying to Firebase.
To create a new application, simply run
ng new [name_of_project], where you replace
name_of_project with the name of your application.
This will create all the files needed to get started. As you can see, Angular CLI has created a lot of files that you would otherwise create yourself in earlier versions, i.e. Angular v1.
Running Your Application
To view your application in the browser, navigate to the project folder and run
ng -serve. This command is used to serve an application locally.
Now navigate to http://localhost:4200/ to see your application in action. Any changes you make to your application are reloaded in your browser, so you don’t have to keep running the application.
Now that we’ve created our app, it’s time to deploy it. We’re going to follow the following steps:
- create a Firebase Project
- install Firebase tools
- build for production
- deploy to Firebase
Creating a Firebase Application
To start, you will need to have a Firebase account. If you don’t have one, go sign up for a free account now.
On the Firebase dashboard, create a new project as shown below. You can simply give it the same name as your Angular app. This will make it easy, especially if you have a lot of projects on the Firebase dashboard.
Install Firebase Command Tools
Firebase makes it easy to set up hosting as it provides you with all the steps to follow along. To install the Firebase command tools, simply run:
Note: You should be in your project directory when you issue this command so that the tools will be installed in your project.
Log in to Firebase.
Answer Yes to the interactive prompt.
Next, the Firebase CLI tool will open a browser where you will be asked to allow Firebase to authenticate via Google Mail.
If the authentication is successful, you will get the following interface in your browser at http://localhost:9005/.
Initialize the Project
The next step is to initialize your Firebase project. This will link your local Angular app to the Firebase application you just created. To do this, simply run:
Choose Hosting as the feature you want to set up for the project since we are only interested in Firebase hosting.
You're about to initialize a Firebase project in this directory:
? Which Firebase CLI features do you want to setup for this folder? Press Space
to select features, then Enter to confirm your choices. Database: Deploy Firebas
e Realtime Database Rules, Hosting: Configure and deploy Firebase Hosting sites
=== Project Setup
First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.
? Select a default Firebase project for this directory: Bucketlist (bucketlist-7
=== Database Setup
Firebase Realtime Database Rules allow you to define how your data should be
structured and when your data can be read from and written to.
? What file should be used for Database Rules? database.rules.json
✔ Database Rules for bucketlist-72e57 have been downloaded to database.rules.json.
Future modifications to database.rules.json will update Database Rules when you run
=== Hosting Setup
Your public directory is the folder (relative to your project directory) that
will contain Hosting assets to be uploaded with firebase deploy. If you
have a build process for your assets, use your build's output directory.
? What do you want to use as your public directory? public
? Configure as a single-page app (rewrite all urls to /index.html)? Yes
✔ Wrote public/index.html
i Writing configuration info to firebase.json...
i Writing project information to .firebaserc...
✔ Firebase initialization complete!
This command will create two files:
These two files contain the Firebase configurations and some important information about your app.
The JSON file should look like this:
Building for Production
Angular CLI provides the
ng build --prod command, which initiates a production build. This command creates a
dist folder which contains all the files for serving the app. This process is important in order to make your app lighter and faster in the loading of web pages. To do this, simply issue:
Deploy the App!
If you’ve followed all the steps until now, our local Angular app is now linked to Firebase, and you can easily push your files the way you do with Git. Simply execute the
firebase deploy command to deploy your app.
=== Deploying to 'bucketlist-72e57'...i deploying database, hostingi database: checking rules syntax...✔ database: rules syntax fordatabase bucketlist-72e57 is validi hosting: preparing public directory forupload...✔ hosting: 1 files uploaded successfullyi database: releasing rules...✔ database: rules fordatabase bucketlist-72e57 released successfully✔
Your app is now deployed, and you can view it by issuing the following command.
firebase open hosting:site
As you have seen, it’s very easy to get started with Firebase as there is very little setup needed to get your app hosted. And it takes much less time than setting up traditional hosting! Angular is a great framework for app development—it has really evolved over the years and each update comes with more advanced features and bug fixes.