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.

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 project 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 project 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.

Next steps

Learn about Firebase: