Add Firebase to your JavaScript project

Follow this guide to use the Firebase JavaScript SDK in your web app or as a client for end-user access, for example, in a Node.js desktop or IoT application.

Step 1: Create a Firebase project and register your app

Before you can add Firebase to your JavaScript app, you need to create a Firebase project and register your app with that project. When you register your app with Firebase, you'll get a Firebase configuration object that you'll use to connect your app with your Firebase project resources.

Visit Understand Firebase Projects to learn more about Firebase projects and best practices for adding apps to projects.

Follow these steps if you're new to Firebase or Google Cloud.
You can also follow these steps if you want to create a wholly new Firebase project (and its underlying Google Cloud project).

  1. Sign into the Firebase console.
  2. Click the button to create a new Firebase project.
  3. In the text field, enter a project name.

    If you're part of a Google Cloud org, you can optionally select which folder you create your project in.

  4. If prompted, review and accept the Firebase terms, then click Continue.
  5. (Optional) Enable AI assistance in the Firebase console (called "Gemini in Firebase"), which can help you get started and streamline your development process.
  6. (Optional) Set up Google Analytics for your project, which enables an optimal experience using these Firebase products: Firebase A/B Testing, Cloud Messaging, Crashlytics, In-App Messaging, and Remote Config (including Personalization).

    Either select an existing Google Analytics account or create a new account. If you create a new account, select your Analytics reporting location, then accept the data sharing settings and Google Analytics terms for your project.

  7. Click Create project.

Firebase creates your project, provisions some initial resources, and enables important APIs. When the process completes, you'll be taken to the overview page for your Firebase project in the Firebase console.

Follow these steps if you want to start using Firebase with an existing Google Cloud project. Learn more about and troubleshoot "adding Firebase" to an existing Google Cloud project.

  1. Sign into the Firebase console with the account that gives you access to the existing Google Cloud project.
  2. Click the button to create a new Firebase project.
  3. At the bottom of the page, click Add Firebase to Google Cloud project.
  4. In the text field, start entering the project name of the existing project, and then select the project from the displayed list.
  5. Click Open project.
  6. If prompted, review and accept the Firebase terms, then click Continue.
  7. (Optional) Enable AI assistance in the Firebase console (called "Gemini in Firebase"), which can help you get started and streamline your development process.
  8. (Optional) Set up Google Analytics for your project, which enables an optimal experience using these Firebase products: Firebase A/B Testing, Cloud Messaging, Crashlytics, In-App Messaging, and Remote Config (including Personalization).

    Either select an existing Google Analytics account or create a new account. If you create a new account, select your Analytics reporting location, then accept the data sharing settings and Google Analytics terms for your project.

  9. Click Add Firebase.

Firebase adds Firebase to your existing project. When the process completes, you'll be taken to the overview page for your Firebase project in the Firebase console.

After you have a Firebase project, you can register your web app with that project.

  1. In the center of the Firebase console's project overview page, click the Web icon () to launch the setup workflow.

    If you've already added an app to your Firebase project, click Add app to display the platform options.

  2. Enter your app's nickname.
    This nickname is an internal, convenience identifier and is only visible to you in the Firebase console.

  3. Click Register app.

  4. Follow the on-screen instructions to add and initialize the Firebase SDK in your app.

    You can also find more detailed instructions for adding, initializing, and using the Firebase SDK in the next steps of this getting started page.

If you don't already have a JavaScript project and just want to try out a Firebase product, you can download one of our quickstart samples.

Step 2: Install the SDK and initialize Firebase

This page describes setup instructions for the Firebase JS SDK's modular API, which uses a JavaScript Module format.

This workflow uses npm and requires module bundlers or JavaScript framework tooling because the modular API is optimized to work with module bundlers to eliminate unused code (tree-shaking) and decrease SDK size.

  1. Install Firebase using npm:

    npm install firebase
  2. Initialize Firebase in your app and create a Firebase App object:

    import { initializeApp } from 'firebase/app';
    
    // TODO: Replace the following with your app's Firebase configuration
    const firebaseConfig = {
      //...
    };
    
    const app = initializeApp(firebaseConfig);

    A Firebase App is a container-like object that stores common configuration and shares authentication across Firebase services. After you initialize a Firebase App object in your code, you can add and start using Firebase services.

    If your app includes dynamic features based on server-side rendering (SSR), you'll need to take some additional steps to ensure that your configuration persists across server rendering and client rendering passes. In your server logic, implement the FirebaseServerApp interface to optimize your app's session management with service workers.

Step 3: Access Firebase in your app

Firebase services (like Cloud Firestore, Authentication, Realtime Database, Remote Config, and more) are available to import within individual sub-packages.

The example below shows how you could use the Cloud Firestore Lite SDK to retrieve a list of data.

import { initializeApp } from 'firebase/app';
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';
// Follow this pattern to import other Firebase services
// import { } from 'firebase/<service>';

// TODO: Replace the following with your app's Firebase configuration
const firebaseConfig = {
  //...
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

// Get a list of cities from your database
async function getCities(db) {
  const citiesCol = collection(db, 'cities');
  const citySnapshot = await getDocs(citiesCol);
  const cityList = citySnapshot.docs.map(doc => doc.data());
  return cityList;
}

Step 4: Use a module bundler (webpack/Rollup) for size reduction

The Firebase Web SDK is designed to work with module bundlers to remove any unused code (tree-shaking). We strongly recommend using this approach for production apps. Tools such as the Angular CLI, Next.js, Vue CLI, or Create React App automatically handle module bundling for libraries installed through npm and imported into your codebase.

See our guide Using module bundlers with Firebase for more information.

Available Firebase services for web

Now that you're setup to use Firebase, you can start adding and using any of the following available Firebase services in your web app.

The following commands show how to import Firebase libraries installed locally with npm. For alternative import options, see the available libraries documentation.

1 Firebase AI Logic was formerly called "Vertex AI in Firebase" with the package firebase/vertexai.

Next steps

Learn about Firebase: