angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

AngularFireModule.initializeApp(...) fails in ng6 prod build

Open jymdman opened this issue 8 years ago • 34 comments

Version info

Angular: ^6.0.0

Firebase: ^5.0.2

AngularFire: 5.0.0-rc.8.1-next

Other (e.g. Ionic/Cordova, Node, browser, operating system):

How to reproduce these conditions

Failing test unit, Plunkr, or JSFiddle demonstrating the problem

Steps to set up and reproduce

AngularFireModule.initializeApp(....) fails when build with prod:

ERROR in Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'AngularFireModule' was called.

NgModule looks like:

@NgModule({
  declarations: [AppComponent, LoginComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    RouterModule.forRoot(APP_ROUTES),
    SharedModule,
    AngularFireModule.initializeApp(environment.firebase, 'stalldata'),
    AngularFirestoreModule,
    AngularFireAuthModule,
    AngularFireStorageModule,
    HomeModule,
    ArtiklarModule,
    MaskinerModule,
    StallbladModule,
    DialogsModule
  ],
  exports: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

Solution is to drop .initializeApp(...) and use providers like this:

@NgModule({
  declarations: [AppComponent, LoginComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    RouterModule.forRoot(APP_ROUTES),
    SharedModule,
    AngularFireModule,
    AngularFirestoreModule,
    AngularFireAuthModule,
    AngularFireStorageModule,
    HomeModule,
    ArtiklarModule,
    MaskinerModule,
    StallbladModule,
    DialogsModule
  ],
  providers: [
    { provide: FirebaseOptionsToken, useValue: environment.firebase },
    { provide: FirebaseAppNameToken, useValue: 'stalldata' },
    { provide: FirebaseAppConfigToken, useValue: undefined }
  ],
  exports: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

Sample data and security rules

<-- include/attach/link to some json sample data (or provide credentials to a sanitized, test Firebase project) -->

Debug output

** Errors in the JavaScript console **

** Output from firebase.database().enableLogging(true); **

** Screenshots **

Expected behavior

Don't know if this is the expected behavior with rc8? If it is, docs should be updated when rc8 is released to match this. (I can create a fix for this)

Actual behavior

jymdman avatar May 13 '18 09:05 jymdman

angularfire2 @next rc8 :+1 firestore.d.ts : constructor(options: FirebaseOptions, config: FirebaseAppConfig, name: string, shouldEnablePersistence: boolean, settings: Settings, platformId: Object, zone: NgZone);

why 7 arguments ?

Celtiore avatar May 13 '18 09:05 Celtiore

Relates to angular/angular#23609

jymdman avatar May 13 '18 11:05 jymdman

It seems like it's not an issue with us but angular, I'll leave this open for now though so people can find it and incase angular gives us any direction. E.g, we should do something different with our metadata.

jamesdaniels avatar May 13 '18 19:05 jamesdaniels

@jamesdaniels But why this issue doesn't appear in rc7 but only rc8. The call signature is same. Or I am missing something?

sarunint avatar May 13 '18 20:05 sarunint

I'm presuming it just has something to do with ng6; but we did change a couple other things since rc7. Namely the addition of AppConfig.

Wait... I might have just thought with something. I did clear out the old overloaded method style as we are using optionals and tslint was complaining about it being unnecessary, maybe that had something to do with it. Let me experiment with that.

jamesdaniels avatar May 13 '18 20:05 jamesdaniels

There also is some suggestions in the angular issue that seems to solve this for others: comment Maybe that is a hack and isn't the right solution...

jymdman avatar May 13 '18 20:05 jymdman

Yeah, no dice on that change; I did it before rc7.

jamesdaniels avatar May 13 '18 21:05 jamesdaniels

I'm having this error too. My temporary fix ( not recommended ) for now is to make aot and buildOptimizer to false in angular.json

brijmcq avatar May 14 '18 04:05 brijmcq

@brijmcq I'd recommend the work-around stated in the issue body in the meantime.

jamesdaniels avatar May 14 '18 07:05 jamesdaniels

@jymdman Thanks for providing the new way to init the firebase config

paulstelzer avatar May 14 '18 09:05 paulstelzer

To me in Ionic 3 I have that problem as I configure the buildOptimizer.?

alsit avatar May 14 '18 12:05 alsit

What is the current workaround (for now)?

sarunint avatar May 14 '18 14:05 sarunint

@sarunint As @jymdman wrote:

Solution is to drop .initializeApp(...) and use providers like this:

@NgModule({
  declarations: [AppComponent, LoginComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    RouterModule.forRoot(APP_ROUTES),
    SharedModule,
    AngularFireModule,
    AngularFirestoreModule,
    AngularFireAuthModule,
    AngularFireStorageModule,
    HomeModule,
    ArtiklarModule,
    MaskinerModule,
    StallbladModule,
    DialogsModule
  ],
  providers: [
    { provide: FirebaseOptionsToken, useValue: environment.firebase }
  ],
  exports: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

paulstelzer avatar May 14 '18 16:05 paulstelzer

FYI #1641 will make this cleaner by making name and appconfig optional.

jamesdaniels avatar May 14 '18 16:05 jamesdaniels

Sorry, where do I locate the types for the providers? This has them but fails to load as Module does not exist here.

import {
  FirebaseOptionsToken,
  FirebaseAppNameToken,
  FirebaseAppConfigToken
} from "@firebase";import {
  FirebaseOptionsToken,
  FirebaseAppNameToken,
  FirebaseAppConfigToken
} from "@firebase";

codercatdev avatar May 14 '18 19:05 codercatdev

@ajonp

import {
  AngularFireModule,
  FirebaseOptionsToken,
  FirebaseAppNameToken,
  FirebaseAppConfigToken
} from 'angularfire2';

sarunint avatar May 14 '18 19:05 sarunint

Humm that throws

message
:
"Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)."

codercatdev avatar May 14 '18 19:05 codercatdev

@ajonp Same here. Getting tired now of firebase constantly breaking my build.

fergalmoran avatar May 14 '18 22:05 fergalmoran

@ajonp @fergalmoran at the providers, just use { provide: FirebaseOptionsToken, useValue: environment.firebase }

where ' environment.firebase' is the path to your firebase config!

If you need a AppNameToken or AppConfigToken, you can use the following (replace undefined (default) with the token you want to use)

        { provide: FirebaseAppNameToken, useValue: undefined },
        { provide: FirebaseAppConfigToken, useValue: undefined }

paulstelzer avatar May 15 '18 07:05 paulstelzer

The truth is all very confusing and it is not clear. What was previously used was AngularFireModule.initializeApp and the firebase config file firebaseConfig was kept as indicated by the documentation now where it should be used. And the modules AngularFireDatabaseModule, AngularFireDatabase that are used to access the base are configured or not, because that is what I can not understand yet. Because in a new project only with angularfire2 and adding in the app.module AngularFireDatabaseModule, AngularFireDatabase the command ionic cordova build android --prod --release is still stuck.

import { BrowserModule } from '@angular/platform-browser'; import { ErrorHandler, NgModule } from '@angular/core'; import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; import { SplashScreen } from '@ionic-native/splash-screen'; import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home';

// import { AngularFireModule } from 'angularfire2'; import { AngularFireDatabaseModule, AngularFireDatabase } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth';

import { AngularFireModule, FirebaseOptionsToken, FirebaseAppNameToken, FirebaseAppConfigToken } from 'angularfire2';

export const firebaseConfig = { apiKey: "AIzaSyC_t5XQmq4O81BOEIPKqRBXAsEcOoXZJmQ", authDomain: "pencaonline.firebaseapp.com", databaseURL: "https://pencaonline.firebaseio.com", projectId: "pencaonline", storageBucket: "pencaonline.appspot.com", messagingSenderId: "698016836224" };

@NgModule({ declarations: [ MyApp, HomePage ], imports: [ BrowserModule, IonicModule.forRoot(MyApp), AngularFireModule, AngularFireAuthModule, AngularFireDatabaseModule ], bootstrap: [IonicApp], entryComponents: [ MyApp, HomePage ], providers: [ StatusBar, SplashScreen, AngularFireDatabase, { provide: FirebaseOptionsToken, useValue: firebaseConfig }, {provide: ErrorHandler, useClass: IonicErrorHandler} ] }) export class AppModule {}

alsit avatar May 15 '18 13:05 alsit