Search code examples
angularfirebaseangular7

Where do I add firebase.initializeApp() in my angular 7 project?


Im trying to deploy my angular app to firebase hosting and I get the following error in console: Uncaught FirebaseError: "projectId" not provided in firebase.initializeApp.

I have this in my app.module.ts:

export class AppModule {
  constructor(private afs: AngularFirestore) {
    afs.firestore.settings({
     timestampsInSnapshots: true,
   });
   afs.firestore.enablePersistence();
   firebase.initializeApp(environment.firebase);
 }
}

Where exactly should I add the initializeApp command in app.module?


Solution

  • There are many ways. To initialize it at the very top, do it as follows

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        firebase.initializeApp(environment.firebase)
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }