skip to Main Content

I’m still trying to use firebase in an Angular project. Here is the error "Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using
provideFirebaseApp) or you’re calling an AngularFire method outside of an NgModule (which is not supported)
"

Before starting, here is my Angular-cli version:

ng --version

     _                      _                 ____ _     ___
    /    _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △  | '_  / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ | | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   __| |_|__, |__,_|_|__,_|_|       ____|_____|___|
                |___/


Angular CLI: 13.0.4
Node: 14.19.0
Package Manager: npm 6.14.16
OS: win32 x64

Angular: 13.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.4
@angular-devkit/build-angular   13.0.4
@angular-devkit/core            13.0.4
@angular-devkit/schematics      13.0.4
@angular/cli                    13.0.4

Theses are the steps, I’m following :

  • Run ng new angular-fire && cd angular-fire
  • I’ve created a project in Firebase (https://console.firebase.google.com) and activated authentication with Google
  • Run ng add @angular/fire (Please, select only the option Authentication. We don’t need the others for the moment)
  • the previous command has updated your app.module.ts, and environment files

my app.module.ts now looks like :

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { initializeApp,provideFirebaseApp } from '@angular/fire/app';
import { environment } from '../environments/environment';
import { provideAuth,getAuth } from '@angular/fire/auth';

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

Now, delete everything in app.component.html and add just a button :

<div>
  <button (click)="signIn()">Sign in with Google</button>
</div>

and in app.component.ts, add signIn function :

import { Component } from '@angular/core';
import {
  getAuth,
  GoogleAuthProvider,
  signInWithPopup,
} from '@angular/fire/auth';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass'],
})
export class AppComponent {
  title = 'angular-fire';

  signIn() {
    return signInWithPopup(getAuth(), new GoogleAuthProvider())
      .then((result) => {
        const user = result.user;
        console.log(user);
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        console.log('errorCode', errorCode);
        console.log('errorMessage', errorMessage);
      });
  }
}

Finally,

  • run ng serve -o
  • open the console of your browser
  • click on the button Sign in with Google
  • you’ll see this error Error: Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using provideFirebaseApp) or you’re calling an AngularFire method outside of an NgModule (which is not supported).

Thanks in advance for your help !

3

Answers


  1. Chosen as BEST ANSWER

    It's working now. First, I have to install "firebase" with npm i firebase Second, I've removed everything in my app.module.ts I'm now using AngularFireModule and AngularFireAuthModule from "compat". See the code and pay attention to the imports

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import { AppComponent } from './app.component';
    import { AngularFireModule } from '@angular/fire/compat';
    import { AngularFireAuthModule } from '@angular/fire/compat/auth';
    import { environment } from '../environments/environment';
    
    @NgModule({
      declarations: [AppComponent],
      imports: [
        BrowserModule,
        AngularFireModule.initializeApp(environment.firebase),
        AngularFireAuthModule,
      ],
      providers: [],
      bootstrap: [AppComponent],
    })
    export class AppModule {}
    

    Next, in app.component.ts also using AngularFireAuth from "@angular/fire/compat/auth"

    import { Component } from '@angular/core';
    import { AngularFireAuth } from '@angular/fire/compat/auth';
    import * as auth from 'firebase/auth';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.sass'],
    })
    export class AppComponent {
      title = 'angular-fire';
    
      constructor(private afAuth: AngularFireAuth) {}
    
      signIn() {
        return this.afAuth
          .signInWithPopup(new auth.GoogleAuthProvider())
          .then((result) => {
            const user = result.user;
            console.log(user);
          })
          .catch((error) => {
            const errorCode = error.code;
            const errorMessage = error.message;
    
            console.log('errorCode', errorCode);
            console.log('errorMessage', errorMessage);
          });
      }
    }
    

    Using ng add @angular/fire may drop you in troubles. You must use all packages from "compat" because the new version of firebase is still under development. So if you use this command and it's not working, try using the compatibility version as explained.


  2. You can try to import the AngularFireModule on app.module.ts. Have you tried it?

    Login or Signup to reply.
  3. To correctly implement Firebase/GA4 in an Angular project so that tracking is done on each route. Install angular/fire using yarn or npm.

    npm i @angular/fire
    

    Then add initialization to import to app.module.ts and define providers.

    import { NgModule } from '@angular/core';
    import { AngularFireModule } from '@angular/fire/compat';
    import {
      AngularFireAnalyticsModule,
      ScreenTrackingService,
      UserTrackingService,
    } from '@angular/fire/compat/analytics';
    
    import { environment } from '@env/environment';
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [AppComponent],
      imports: [
        AppRoutingModule,
        AngularFireModule.initializeApp(environment.firebase),
        AngularFireAnalyticsModule,
      ],
      providers: [ScreenTrackingService, UserTrackingService],
      bootstrap: [AppComponent],
    })
    export class AppModule {}
    

    Remember to add Firebase settings to the project configuration (environment variables).

       firebase: {
        apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXX',
        authDomain: 'XXXXXX-analytics.firebaseapp.com',
        projectId: 'XXXXX-analytics',
        storageBucket: 'XXXXXX-analytics.appspot.com',
        messagingSenderId: 'CXXXXXX',
        appId: 'XXXXXX',
        measurementId: 'G-XXXXXXXX',
      },
    

    The ScreenTrackingService and UserTrackingService providers ensure that default events are sent to Firebase on each route.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search