Comment on page
You'll get started with Firebase Tools by installing it. Open up your terminal and make sure you have Node.js installed:
$ node --version
$ #some version number
Now it's time to install Firebase Tools.
$ npm -g install firebase-tools
To confirm successful installation:
$ firebase --version
$ #some version number
To get help:
$ firebase --help
We mostly use Firebase Tools to initialize our apps. Just make a new folder,
cdinto it, and call
Select all of the Firebase features. Because why not!
Now follow the prompts, sticking to the defaults with one exception:
? Configure as a single-page app (rewrite all urls to /index.html)? (y/N) y
It's easy to remove the single-page app rewrites later if you don't need them. Your should
firebase.jsonfile should look something like this:
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
firebase deployto deploy your new project.
You may get an error like...
Error: HTTP Error: 400, Project 'how-to-firebase-tutorials' is not a Firestore enabled project.
... in which case, you'll need to use your Console to activate the Firestore Beta. Make sure to start in locked mode.
firebase deployagain if necessary.
Notice the final line of output that looks like this:
Hosting URL: https://how-to-firebase-tutorials.firebaseapp.com
Follow that url to see the results!
The Node.js community has moved toward putting all deploy commands in a
package.jsonfile, so let's initialize our project for Node.js as well and set up our deploy pipeline.
You likely have
./functions/because you ran
firebase initand initialized the functions. The following steps will add new
npm-related files in your root directory (
./package.jsonand such). This is intended, because both folders will act as separate NPM packages.
$ npm init #go ahead and accept the defaults
We've already installed Firebase Tools globally with
npm -g install firebase tools. Now that we have a local
package.jsonfile, let's install it locally as well.
npm install --save-dev firebase-tools
Now that we have a local version of
package.jsonscripts will run using the local version instead of the global version. This is super helpful if you copy your code to a machine that doesn't have Firebase Tools installed globally.
Now let's add some scripts:
"deploy": "firebase deploy",
"deploy:hosting": "firebase deploy --only hosting",
"deploy:database": "firebase deploy --only database",
"deploy:firestore": "firebase deploy --only firestore",
"deploy:storage": "firebase deploy --only storage",
"deploy:functions": "firebase deploy --only functions"
We've added a
scriptsattribute and created scripts for each kind of deploy that we could use. We won't need to call
firebase deploydirectly anymore. Instead we'll call
npm run deployor
npm run deploy:hosting.
This is a great habit to promote. It both documents and standardizes your deploy pipeline so that you can come back to this project in two years and know exactly how to deploy it.
npm run deploy:hostingto test it out!
Firebase Tools deploys five different modules, each of which can be deployed individually with the
Hosting deploys your public folder to Firebase Hosting. It also deploys any hosting settings you have in
The database module deploys your security rules to the Realtime Database as defined in
The firestore module deploys two files,
firestore.indexes.json. These files contain security rules and index specifications.
The storage module deploys the security rules defined in
storage.rules, which you'll use to secure Firebase Storage.
The functions module is the trickiest of the bunch. It runs a bunch of checks on your
/functionsfolder to make sure that all of the right NPM packages are installed and that
/functions/index.jsexports valid Cloud Functions.
Firebase Tools will deploy your functions to Cloud Functions once its validation checks pass. But beware! Firebase Tools can't validate your code very deeply. It just makes sure that you're importing modules correctly and attaching functions to valid endpoints.
We HIGHLY recommend--in all caps no less--developing Cloud Functions in a local testing environment. Don't get sucked into the tempting feedback loop of deploying a function, testing it on the Cloud Functions servers, making edits and deploying again... This is your ticket to Cloud Functions hell 😈
We'll cover Cloud Functions test-driven development elsewhere. You have been warned! 🎉🎉🎉