Walkthrough
Last updated
Last updated
You'll get started with Firebase Tools by installing it. Open up your terminal and make sure you have Node.js installed:
If you see bash: command not found: node
, then you'll need to install Node.js.
Now it's time to install Firebase Tools.
To confirm successful installation:
To get help:
We mostly use Firebase Tools to initialize our apps. Just make a new folder, cd
into it, and call firebase init
.
Select all of the Firebase features. Because why not!
Now follow the prompts, sticking to the defaults with one exception:
It's easy to remove the single-page app rewrites later if you don't need them. Your should firebase.json
file should look something like this:
Run firebase deploy
to deploy your new project.
You may get an error like...
... in which case, you'll need to use your Console to activate the Firestore Beta. Make sure to start in locked mode.
Now try firebase deploy
again if necessary.
Notice the final line of output that looks like this:
Follow that url to see the results!
The Node.js community has moved toward putting all deploy commands in a package.json
file, so let's initialize our project for Node.js as well and set up our deploy pipeline.
You likely have
package.json
and othernpm
files under./functions/
because you ranfirebase init
and initialized the functions. The following steps will add newnpm
-related files in your root directory (./package.json
and such). This is intended, because both folders will act as separate NPM packages.
We've already installed Firebase Tools globally with npm -g install firebase tools
. Now that we have a local package.json
file, let's install it locally as well.
Now that we have a local version of firebase-tools
our package.json
scripts 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:
We've added a scripts
attribute and created scripts for each kind of deploy that we could use. We won't need to call firebase deploy
directly anymore. Instead we'll call npm run deploy
or 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.
Try running npm run deploy:hosting
to test it out!
Firebase Tools deploys five different modules, each of which can be deployed individually with the --only
flag:
hosting
database
firestore
storage
functions
Hosting deploys your public folder to Firebase Hosting. It also deploys any hosting settings you have in firebase.json
.
The database module deploys your security rules to the Realtime Database as defined in database.rules.json
.
The firestore module deploys two files, firestore.rules
and 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 /functions
folder to make sure that all of the right NPM packages are installed and that /functions/index.js
exports 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! 🎉🎉🎉