Skip to content

AngularFireModule from @angular/fire/compat not providing the database appropriately. #3683

Description

@isaac-mutuku-irungu

So I've been facing an issue for the past couple of days where whenever I try to use the AngularFireDatabase database to read or write to my Firebase database my console gives me the error:

Image

No provider found for Database2. Source: Standalone[_Homecomponent].

I'm currently running Angular 20.0 as AngularFire 21.0 is yet to be fully fleshed out.

My dummy service to test the data looks like this below:

Image

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/compat/database';
@Injectable({
providedIn: 'root'
})
export class Postingservice {
constructor(private afDb:AngularFireDatabase){}
viewPosts(){
return this.afDb.list("/posts").valueChanges();
}
}

I've both tried instantiating the afDb in the constructor and using the inject() function and neither have worked to any avail.

Here also is my app.config.ts:

Image

import { ApplicationConfig, provideBrowserGlobalErrorListeners, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';

import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { getDatabase, provideDatabase } from '@angular/fire/database';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideFirebaseApp(() =>
initializeApp({
// FIREBASE CONFIG DATA
})),
provideDatabase(() => getDatabase()),
]
};

I've even provided the provideDatabase() function even as it's not necessary for the AngularFireDatabase as a provider and likewise it's been to no avail...

And my application is bootstrapping correctly as it runs just fine until I create an instance of the afDb database in a component where I then get the above stated error. Kindly help.

Activity

  1. added
    comp: compatCompatibility layer for the pre-modular API (src/compat).
    needs: more infoWaiting on the reporter for repro, versions, or detail.
    type: bugDefect: expected behavior doesn't happen.
    on Jul 25, 2026
  2. armando-navarro commented on Jul 26, 2026

    @armando-navarro
    Collaborator

    Thanks for the report.

    The thing to flag first: Database2 is the name the bundler gives AngularFire's modular Database DI token, so this error is about the modular Database provider, but your pasted service injects the compat AngularFireDatabase from @angular/fire/compat/database. Those two do not line up, and that mismatch is the useful clue here.

    The compat AngularFireDatabase does not depend on the modular Database token at all; it depends on FIREBASE_OPTIONS, which comes from AngularFireModule (compat), not from your modular provideDatabase. So the compat service as pasted would fail looking for FIREBASE_OPTIONS, not with a Database2 error. And since you have already added provideDatabase(() => getDatabase()) at the root, a plain modular Database injection at the root should resolve, so "no provider" is surprising.

    That points at one of a couple of things, and both assume something in your app is injecting the modular Database token (the compat service you pasted does not, which is itself the first thing to sort out):

    • Two copies of @angular/fire in the tree: the Database class your code imports is then a different class object than the one provideDatabase registered, so the injector has no provider for the exact token you asked for. npm ls @angular/fire will show a duplicate. (A duplicate firebase on its own is a different, runtime symptom, not this DI error.)
    • Less likely, a lazy-loaded route or child injector that re-declares its own Database provider and so shadows the root one.

    To actually fix it, pick one API and stay in it. compat and modular are separate DI systems:

    • Modular (matches the provideDatabase(() => getDatabase()) you already have): inject the modular token and use modular functions. private db = inject(Database) from @angular/fire/database, then ref(this.db, '/path') with objectVal/list/listVal. Drop the compat import.
    • compat (if you specifically want AngularFireDatabase.list(...).valueChanges()): provide the compat modules instead, importProvidersFrom(AngularFireModule.initializeApp(environment.firebase), AngularFireDatabaseModule), and remove provideDatabase.

    Could you share npm ls @angular/fire and confirm which service actually throws? Then I can point you at the exact one-line fix rather than guessing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp: compatCompatibility layer for the pre-modular API (src/compat).needs: more infoWaiting on the reporter for repro, versions, or detail.type: bugDefect: expected behavior doesn't happen.version: current (v17+)Targets the current modular API (v17+).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions