Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Package path ./compat is not exported from package

Note: I think I was able to to reproduce this (see stackblitz example). The error there is printed in the console as "INTERNAL ASSERTION FAILED: Expected a class definition". This is different to what I get locally but to me this looks like the same issue.

Anyway, just comment out line 15 in app.component.ts and the error will disappear.

I am trying to get started with Firebase but when I install & compile the Angular project I am getting the following error:

Module not found: Error: Package path ./compat is not exported from package /home/sfalk/workspaces/web-mobile/node_modules/firebase (see exports field in /home/sfalk/workspaces/web-mobile/node_modules/firebase/package.json)

The interesting thing is that I am only getting this error when I am injecting my AuthService e.g. like this:

Crashes

import {Component, OnInit} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {AuthService} from "../../../services/auth.service";

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

  public loading = false;

  constructor(
    private formBuilder: FormBuilder,
    public authService: AuthService,
  ) {

  }
  
}

Does not crash

import {Component, OnInit} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {AuthService} from "../../../services/auth.service";

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

  public loading = false;

  constructor(
    // private formBuilder: FormBuilder,
    // public authService: AuthService,
  ) {

  }
  
}

AuthService

I am not sure if this class is causing the issue, I highly doubt it, but here is the code. Nothing special.

The only thing to mention maybe is that I am importing from @angular/fire/compat which is as intended according to the docs.

import {Injectable} from "@angular/core";
import {Observable, of} from "rxjs";
import {Router} from "@angular/router";
import firebase from "firebase/compat";
import {switchMap} from "rxjs/operators";
import {AngularFireAuth} from "@angular/fire/compat/auth";
import {AngularFirestore, AngularFirestoreDocument} from "@angular/fire/compat/firestore";
import User = firebase.User;
import auth = firebase.auth;


@Injectable({providedIn: 'root'})
export class AuthService {
  public user$: Observable<User | null>;

  constructor(
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore,
    private router: Router
  ) {
    this.user$ = this.afAuth.authState.pipe(
      switchMap(user => {
        if (user) {
          return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
        }
        return of(null);
      })
    ) as Observable<User>;
  }

  async googleSignIn() {
    const provider = new auth.GoogleAuthProvider();
    const credentials = await this.afAuth.signInWithPopup(provider);
    return this.updateUserData(credentials.user);
  }

  async signOut() {
    await this.afAuth.signOut();
    return this.router.navigate(['/'])
  }

  // noinspection TypeScriptValidateTypes
  private updateUserData(user: User | null) {
    if (!user) {
      throw "User is null."
    }
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);

    const data: any = {
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      photoURL: user.photoURL
    }

    return userRef.set(data, {merge: true});
  }
}

In my package.json:

"@angular/fire": "^7.0.3",
"firebase": "^9.0.1",

See also

  • Github issue
like image 400
Stefan Falk Avatar asked Dec 07 '22 09:12

Stefan Falk


1 Answers

You need to change your import from:

import firebase from "firebase/compat";

to

import firebase from "firebase/compat/app";
//                                    ^^^

Getting started with Firebase Authentication

like image 120
Jimmy Majed Avatar answered Jan 12 '23 05:01

Jimmy Majed