Firebase JavaScript SDK
The Firebase JavaScript SDK provides a comprehensive client-side library for interacting with Google's Firebase platform, suitable for web, mobile-web (e.g., React Native, Ionic), and Node.js desktop (e.g., Electron) applications. The current stable version is 12.12.0, with minor versions released frequently, often weekly or bi-weekly, reflecting continuous development and updates across its sub-packages. It offers a suite of services including Authentication, Cloud Firestore, Realtime Database, Cloud Storage, Cloud Functions, Messaging, Performance Monitoring, Analytics, Remote Config, App Check, and AI capabilities. A key differentiator since version 9 is its modular, tree-shakable API, which significantly improves application bundle sizes. This SDK is distinct from the Firebase Admin Node.js SDK, which is designed for privileged server environments and administrative access.
Common errors
-
Firebase: No Firebase App '[DEFAULT]' has been created - call initializeApp() first (app/no-app).
cause Attempting to use a Firebase service (e.g., getAuth(), getFirestore()) before `initializeApp()` has been successfully called.fixEnsure `initializeApp(firebaseConfig)` is called once at the start of your application's lifecycle before any Firebase service-specific functions are invoked. This call should typically be near your app's entry point. -
Error: Modular code path not found for firebase/auth. Did you mean firebase/compat/auth?
cause This error occurs when a bundler or runtime cannot find the expected modular entry point, often due to an incorrect import path or mixing modular imports with the older 'compat' API usage.fixVerify that your import statements are correct for the modular API (e.g., `import { getAuth } from 'firebase/auth';`). If you intend to use the compat API, explicitly import from `firebase/compat/auth`. -
TypeError: __webpack_require__.i(...) is not a function
cause This Webpack-specific error (or similar from other bundlers) often arises when using `require('firebase/app')` or an older CommonJS pattern in a project configured for ES Modules (ESM) with the modern Firebase SDK.fixSwitch all Firebase imports to the ES Module syntax (e.g., `import { initializeApp } from 'firebase/app';`). Ensure your bundler configuration (e.g., Webpack, Rollup, Vite) is correctly set up to handle ESM.
Warnings
- breaking Version 9 introduced a completely redesigned modular API to enable tree-shaking and reduce bundle sizes. The previous 'compat' API still works but is not tree-shakable.
- deprecated All Imagen models provided by the `@firebase/ai` package are officially deprecated and will be shut down as early as June 2026.
- deprecated The `sendMediaChunks()` and `sendMediaStream()` methods within the `@firebase/ai` package's `LiveSession` class have been deprecated.
- gotcha Firebase Authentication for React Native now requires `react-native-async-storage` v2+ as a peer dependency, which may require an update to your `package.json`.
Install
-
npm install firebase -
yarn add firebase -
pnpm add firebase
Imports
- initializeApp
const firebase = require('firebase/app'); import firebase from 'firebase';import { initializeApp } from 'firebase/app'; - getAuth
import firebase from 'firebase/auth'; // Incorrect modular import import { auth } from 'firebase'; // Old compat API patternimport { getAuth, signInWithEmailAndPassword } from 'firebase/auth'; - getFirestore
const firestore = require('firebase/firestore').firestore; // CommonJS with modular API import { firestore } from 'firebase'; // Old compat API patternimport { getFirestore, collection, addDoc } from 'firebase/firestore'; - Auth types
import type { User } from 'firebase/auth';
Quickstart
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, addDoc } from 'firebase/firestore';
// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID",
// Optional: measurementId: "G-XXXXXXXXXX"
};
async function initializeAndAddData() {
try {
const app = initializeApp(firebaseConfig);
console.log("Firebase app initialized successfully.");
const db = getFirestore(app);
const docRef = await addDoc(collection(db, "users"), {
first: "Ada",
last: "Lovelace",
born: 1815
});
console.log("Document written with ID: ", docRef.id);
} catch (error: any) {
console.error("Error initializing Firebase or adding document:", error.message);
}
}
initializeAndAddData();