Firebase CLI

Firebase CLI cheatsheet — deploy hosting, functions, configure projects, run emulators. firebase deploy, firebase init, firebase emulators:start. Full CLI reference.

6 min read

What it is

The Firebase CLI is a command-line interface for managing Firebase projects, deploying web apps, and interacting with Firebase services. You reach for it when you need to automate Firebase deployments, manage configurations, or run Firebase emulators locally.

Installation

Node.js and npm: The Firebase CLI is installed via npm, which comes bundled with Node.js.

Install Firebase CLI:

  • Linux/macOS:
    npm install -g firebase-tools
    
  • Windows:
    npm install -g firebase-tools
    

Login to Firebase:

firebase login

This opens a browser window for you to authenticate with your Google account and authorize the CLI to access your Firebase projects.

Core Concepts

  • Firebase Project: A central hub for your Firebase services. The CLI interacts with a specific Firebase project configured in your local directory.
  • Firebase Hosting: A service for hosting your static web assets (HTML, CSS, JavaScript, images).
  • Firebase Functions: Serverless backend code that runs in response to events.
  • Firebase Emulators: Local emulators for Firebase services (Authentication, Firestore, Realtime Database, Functions, etc.) allowing for offline development and testing.
  • firebase.json: A configuration file in your project root that defines deployment settings, rewrites, and other Firebase-specific configurations.
  • firestore.rules / database.rules.json: Files containing security rules for Firestore and Realtime Database, respectively.

Commands / Usage

Project Management

  • Initialize a new Firebase project in a directory:

    firebase init
    

    Walks you through setting up a project, selecting services (Hosting, Functions, etc.), and configuring them.

  • Initialize a specific Firebase service:

    firebase init hosting
    firebase init functions
    firebase init firestore
    firebase init database
    firebase init storage
    firebase init emulators
    

    Initializes only the specified service within an existing Firebase project directory.

  • Log in to your Firebase account:

    firebase login
    

    Authenticates the CLI with your Google account.

  • Log out of your Firebase account:

    firebase logout
    
  • List your Firebase projects:

    firebase projects:list
    

    Shows all Firebase projects associated with your logged-in account.

  • Set the default Firebase project for the current directory:

    firebase use <project-id>
    

    Associates your local directory with a specific Firebase project. You can find <project-id> from firebase projects:list.

  • View the currently selected Firebase project:

    firebase use
    

Deployment

  • Deploy all Firebase services configured in firebase.json:

    firebase deploy
    

    Deploys Hosting, Functions, Firestore rules, etc., as defined in your firebase.json.

  • Deploy only Firebase Hosting:

    firebase deploy --only hosting
    
  • Deploy only Firebase Functions:

    firebase deploy --only functions
    
  • Deploy specific functions:

    firebase deploy --only functions:myFunction,anotherFunction
    
  • Deploy Firestore security rules:

    firebase deploy --only firestore:rules
    
  • Deploy a specific directory to Firebase Hosting:

    firebase deploy --project my-firebase-project --public /path/to/your/public/dir
    

    (Note: firebase init hosting usually sets up the public directory in firebase.json, making this less common. Use firebase deploy --only hosting for standard deployments.)

  • Deploy to a specific Firebase project (overriding firebase use):

    firebase deploy --project my-other-firebase-project
    
  • Deploy with a specific version tag for Hosting:

    firebase deploy --version my-deploy-version-1
    

    Useful for tracking deployments to Firebase Hosting.

  • Deploy and automatically open the deployed site:

    firebase deploy --hosting-site my-site-id --open
    

    Use this if you have multiple hosting sites configured for your project.

Emulators

  • Start all configured Firebase emulators:

    firebase emulators:start
    

    Starts emulators for services defined in firebase.json.

  • Start specific Firebase emulators:

    firebase emulators:start --only firestore,functions
    
  • Export emulator data:

    firebase emulators:export ./path/to/export/dir
    

    Saves the current state of emulated databases (Firestore, Realtime Database).

  • Import emulator data:

    firebase emulators:import ./path/to/import/dir
    

    Loads previously exported emulator data.

  • Emulators UI: By default, the emulators UI is accessible at http://localhost:4000.

Functions Management

  • Deploy Cloud Functions: (Covered in Deployment section)

  • List deployed Cloud Functions:

    firebase functions:list
    
  • Delete a deployed Cloud Function:

    firebase functions:delete myFunctionName
    
  • Get logs for Cloud Functions:

    firebase functions:log
    

    Fetches recent logs for all functions in your project.

  • Get logs for a specific Cloud Function:

    firebase functions:log --only myFunction
    
  • Get logs with a time filter:

    firebase functions:log --since 1h
    

    Fetches logs from the last hour. Supports 10m, 1h, 1d.

Hosting Management

  • Deploy Hosting content: (Covered in Deployment section)

  • List deployed Hosting sites:

    firebase hosting:sites:list
    
  • Create a new Hosting site:

    firebase hosting:sites:create my-new-site-id
    
  • Delete a Hosting site:

    firebase hosting:sites:delete my-site-id
    
  • List Hosting deployments for a site:

    firebase hosting:releases:list --site my-site-id
    
  • Get details of a specific Hosting release:

    firebase hosting:releases:get <release-id> --site my-site-id
    
  • Delete a Hosting release:

    firebase hosting:releases:delete <release-id> --site my-site-id
    

Database Management (Firestore & Realtime Database)

  • Deploy Firestore rules:

    firebase deploy --only firestore:rules
    
  • Deploy Realtime Database rules:

    firebase deploy --only database:rules
    
  • Get Firestore rules:

    firebase firestore:rules:get
    
  • Get Realtime Database rules:

    firebase database:rules:get
    
  • Import data to Firestore (from JSON file):

    firebase firestore:import data.json
    

    (Requires emulators or specific setup for production imports, typically done via SDKs.)

  • Export data from Firestore (to JSON file):

    firebase firestore:export /path/to/export/dir
    

    (Requires emulators or specific setup for production exports, typically done via SDKs.)

Other Utilities

  • Get Firebase project configuration:

    firebase app:list
    

    Lists the Firebase apps (iOS, Android, Web) associated with your project.

  • Get the Firebase CLI version:

    firebase --version
    

Common Patterns

  • Deploying and immediately testing locally with emulators:

    firebase emulators:start & firebase deploy --only functions && firebase deploy --only hosting
    

    Starts emulators in the background, deploys functions, then deploys hosting.

  • Checking function logs after a deployment:

    firebase deploy --only functions && firebase functions:log
    
  • Setting up a new project with Hosting and Functions:

    firebase init
    # Select Hosting, then Functions
    # Configure public directory for Hosting (e.g., 'public')
    # Configure Functions runtime (e.g., Node.js 16)
    # Then deploy
    firebase deploy
    
  • Deploying a specific function and then viewing its logs:

    firebase deploy --only functions:myApiFunction
    firebase functions:log --only myApiFunction --limit 50
    
  • Managing multiple hosting sites:

    # Create a new site
    firebase hosting:sites:create staging-site
    # Update firebase.json to include the new site configuration
    # Deploy to the staging site
    firebase deploy --only hosting:staging-site
    # Deploy to the default site
    firebase deploy --only hosting
    

Gotchas

  • Project ID vs. Project Name: Be careful when using --project. The CLI expects the Project ID (e.g., my-cool-project-12345), not the human-readable project name.
  • firebase.json Configuration: If you encounter unexpected deployment behavior, always double-check your firebase.json for correct paths, rewrites, and service configurations.
  • Emulator State: Emulator data is persistent by default within a session. Use firebase emulators:export and firebase emulators:import or the emulator UI to manage state between sessions if needed. Deleting and restarting emulators usually clears their state.
  • Functions Cold Starts: When deploying functions, remember that they can experience cold starts. Testing locally with emulators helps mitigate this during development, but it’s a factor in production.
  • Global vs. Local Installation: If you install firebase-tools globally (npm install -g firebase-tools), you can run firebase from any directory. If you install it as a dev dependency in your package.json (npm install --save-dev firebase-tools), you’ll need to run it via npx firebase or an npm script. Global installation is generally recommended for ease of use.
  • CI/CD Authentication: For automated deployments in CI/CD pipelines, use firebase login:ci to generate a token instead of firebase login which requires interactive browser authentication. Store this token securely.