iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Firebase CLI

Firebase CLI: log in, init a project, deploy hosting + functions + rules, run emulators. The shell side of Firebase.

Firebase — CLI

EXAMPLE
# ===== Install =====
npm install -g firebase-tools
firebase --version

# Or via standalone binary:
curl -sL https://firebase.tools | bash

# ===== Login =====
firebase login                # OAuth in browser
firebase login:list           # show signed-in accounts
firebase logout

# CI / scripts:
firebase login:ci             # gives a long-lived token (treat as secret!)

# Or service account:
export GOOGLE_APPLICATION_CREDENTIALS=./service-account.json
firebase ...

# ===== Initialise =====
firebase init
# Interactive: pick services (Hosting, Functions, Firestore, Storage, Emulators, ...)
# Creates firebase.json + service-specific config files.

# ===== Project aliases =====
firebase use --add            # link a local alias to a project
firebase use staging          # switch alias
firebase projects:list

# Pin per-folder in .firebaserc.

# ===== Deploy =====
firebase deploy                                 # everything in firebase.json
firebase deploy --only hosting
firebase deploy --only functions
firebase deploy --only firestore:rules,firestore:indexes
firebase deploy --only storage:rules
firebase deploy --only functions:sendEmail      # one specific function

# ===== Hosting =====
firebase hosting:channel:deploy preview-1       # preview channel + temp URL
firebase hosting:channel:list
firebase hosting:channel:delete preview-1
firebase hosting:disable                        # remove the live site temporarily

# ===== Functions =====
cd functions
npm run build                                   # if TS
firebase deploy --only functions

# Logs:
firebase functions:log
firebase functions:log --only sendEmail
firebase functions:log --since 30m

# Delete a function:
firebase functions:delete sendEmail

# ===== Firestore =====
firebase firestore:indexes      # see current
firebase deploy --only firestore:indexes

# Backup + restore (gcloud):
gcloud firestore export gs://my-backup-bucket/snapshot
gcloud firestore import gs://my-backup-bucket/snapshot

# ===== Emulators (local dev + CI) =====
firebase emulators:start
# Default: auth (9099), firestore (8080), functions (5001), hosting (5000), pubsub, ...

# With imported state:
firebase emulators:start --import=./emu-state --export-on-exit

# Run tests against emulators:
firebase emulators:exec --only firestore 'npm test'

# Wire your SDK:
import { connectFirestoreEmulator } from 'firebase/firestore';
if (location.hostname === 'localhost') connectFirestoreEmulator(db, 'localhost', 8080);

# ===== Hosting redirects + rewrites =====
# firebase.json
{
  "hosting": {
    "public": "public",
    "rewrites": [
      { "source": "/api/**", "function": "api" },
      { "source": "**", "destination": "/index.html" }
    ],
    "redirects": [
      { "source": "/old", "destination": "/new", "type": 301 }
    ]
  }
}

# ===== Patterns to internalise =====
# - One project alias per environment (dev / staging / prod)
# - Emulator suite in CI for unit + integration tests
# - Preview channels for every PR
# - Deploy --only the things that changed for fast iteration

# ===== Pitfalls =====
# - firebase login:ci tokens checked into git
# - Deploying functions without 'npm run build' in TS projects
# - Forgetting to upload Firestore indexes (queries fail at runtime in prod)
# - Project alias mismatched with the active terminal -> deploy to wrong env

Why it matters

The CLI is where most Firebase work happens once the console is configured. Login, pick a project alias, deploy --only the slices you changed, and use emulators for local + CI tests. Preview channels deserve their own bullet point — they make every PR demoable for free.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
firebase login
firebase init
firebase deploy --only hosting,functions
firebase emulators:start
Try it Yourself »

Exercise

Install the CLI globally.

npm install -g

Discussion

Loading…