Description
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:
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:
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:
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
armando-navarro commentedon Jul 26, 2026
Thanks for the report.
The thing to flag first:
Database2is the name the bundler gives AngularFire's modularDatabaseDI token, so this error is about the modularDatabaseprovider, but your pasted service injects the compatAngularFireDatabasefrom@angular/fire/compat/database. Those two do not line up, and that mismatch is the useful clue here.The compat
AngularFireDatabasedoes not depend on the modularDatabasetoken at all; it depends onFIREBASE_OPTIONS, which comes fromAngularFireModule(compat), not from your modularprovideDatabase. So the compat service as pasted would fail looking forFIREBASE_OPTIONS, not with aDatabase2error. And since you have already addedprovideDatabase(() => getDatabase())at the root, a plain modularDatabaseinjection 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
Databasetoken (the compat service you pasted does not, which is itself the first thing to sort out):@angular/firein the tree: theDatabaseclass your code imports is then a different class object than the oneprovideDatabaseregistered, so the injector has no provider for the exact token you asked for.npm ls @angular/firewill show a duplicate. (A duplicatefirebaseon its own is a different, runtime symptom, not this DI error.)Databaseprovider and so shadows the root one.To actually fix it, pick one API and stay in it. compat and modular are separate DI systems:
provideDatabase(() => getDatabase())you already have): inject the modular token and use modular functions.private db = inject(Database)from@angular/fire/database, thenref(this.db, '/path')withobjectVal/list/listVal. Drop the compat import.AngularFireDatabase.list(...).valueChanges()): provide the compat modules instead,importProvidersFrom(AngularFireModule.initializeApp(environment.firebase), AngularFireDatabaseModule), and removeprovideDatabase.Could you share
npm ls @angular/fireand confirm which service actually throws? Then I can point you at the exact one-line fix rather than guessing.armando-navarro commentedon Nov 5, 2026
Coming back to this with a correction, and sorry for the long gap.
I re-read this issue thread, and the error in your screenshot is
NG0201: No provider found for InjectionToken angularfire2.app.options. Path: _AngularFireDatabase -> InjectionToken angularfire2.app.options.That token is the compat layer's
FIREBASE_OPTIONS, and the only thing that provides it isAngularFireModule.initializeApp(...).The duplicate-copies and child-injector theories in my earlier comment do not apply here, and there is no need for the
npm lsoutput I asked for.I had been dealing with so many seemingly unrelated issues at the time that were all caused by duplicate package copies, that my mind went in that direction when it shouldn't have.
The fix
Add one line to the
providersarray inapp.config.ts:importProvidersFromcomes from@angular/coreandAngularFireModulefrom@angular/fire/compat.provideFirebaseAppandprovideDatabasecan stay. Both setups end up sharing one Firebase app and one Database instance, so keeping them only matters if you also use the modular API somewhere.If you only use the compat API, you can instead replace the two modular lines with
importProvidersFrom(AngularFireModule.initializeApp({ ... }), AngularFireDatabaseModule), with the module from@angular/fire/compat/database.I ran your service and config exactly as pasted on Angular 20 with AngularFire 20.0.1 and on Angular 21 with 21.0.0. The error reproduces in both, and the added line resolves the service in both.
One more thing you will hit
With the provider in place,
afDb.list()(andobject()andcreatePushId()) throws this when it is called from anywhere other than a constructor or a field initializer, for example fromngOnInitor a click handler:Until it ships, either:
viewPosts()from a constructor or a field initializer, orprivate injector = inject(EnvironmentInjector), and wrap the call:The capture has to happen in the constructor, not inline in the later call, or it throws the same error.
The setup steps for the compat API have also been missing from our docs since the modular rewrite, which is how a config ends up without the compat module in the first place.
I will fix that too, and I am going to retitle this issue so it tracks the docs change.
If the added line does not resolve it for you, please post the new console error and I will dig back in.
[-]AngularFireModule from @angular/fire/compat not providing the database appropriately.[/-][+]Docs: the compat setup (`AngularFireModule.initializeApp`) has had no page since the modular rewrite, and no standalone example[/+]docs(compat): add a setup section in NgModule and standalone form