Alternative ways to add Firebase to your JavaScript project

For most Firebase Web apps we strongly recommend using the SDK via npm. However, for users with special requirements, Firebase provides alternative ways to add the SDK. This page provides detailed setup instructions for these alternative methods:

  • CDN (content delivery network)
  • npm for Node.js apps

Using these methods, you can add any of the available libraries to your app.

From the CDN

You can configure partial import of the Firebase JavaScript SDK and only load the Firebase products that you need. Firebase stores each library of the Firebase JavaScript SDK on our global CDN (content delivery network).

  1. To include only specific Firebase products (for example, Authentication and Cloud Firestore), add the following script to the bottom of your <body> tag, but before you use any Firebase services:

    <body>
      <!-- Insert this script at the bottom of the HTML, but before you use any Firebase services -->
      <script type="module">
        import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.14.0/firebase-app.js'
    
        // If you enabled Analytics in your project, add the Firebase SDK for Google Analytics
        import { getAnalytics } from 'https://www.gstatic.com/firebasejs/10.14.0/firebase-analytics.js'
    
        // Add Firebase products that you want to use
        import { getAuth } from 'https://www.gstatic.com/firebasejs/10.14.0/firebase-auth.js'
        import { getFirestore } from 'https://www.gstatic.com/firebasejs/10.14.0/firebase-firestore.js'
      </script>
    </body>
  2. Add your Firebase configuration object, and then initialize Firebase in your app:

    <body>
      <script type="module">
        // ...
    
        // TODO: Replace the following with your app's Firebase project configuration
        const firebaseConfig = {
          // ...
        };
    
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
      </script>
    </body>

Node.js apps

  1. Install the Firebase JavaScript SDK:

    1. If you don't already have a package.json file, create one by running the following command from the root of your JavaScript project:

      npm init
    2. Install the firebase npm package and save it to your package.json file by running:

      npm install --save firebase@10.14.0
  2. Use one of the following options to use the Firebase module in your app:

    • You can require modules from any JavaScript file

      To include only specific Firebase products (like Authentication and Cloud Firestore):

      // Firebase App (the core Firebase SDK) is always required and
      // must be listed before other Firebase SDKs
      var firebase = require("firebase/app");
      
      // Add the Firebase products that you want to use
      require("firebase/auth");
      require("firebase/firestore");
      


    • You can use ES2015 to import modules

      To include only specific Firebase products (like Authentication and Cloud Firestore):

      // Firebase App (the core Firebase SDK) is always required and
      // must be listed before other Firebase SDKs
      import firebase from "firebase/app";
      
      // Add the Firebase services that you want to use
      import "firebase/auth";
      import "firebase/firestore";
      
  3. Add your Firebase configuration object, and then initialize Firebase in your app:

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