3

I am trying to integrate Firebase into my Angular 2 app but I keep getting this error that Firebase is not a constructor. Following is what I did:

  1. Installed firebase using bower
  2. Referenced the script in index.html.
  3. Install firebase ambient typings
  4. Created a Firebase service
  5. Provided it through bootstrap()
  6. Had it injected in a component

FirebaseService.ts

@Injectable()
export class FirebaseService
{
    dataRef: Firebase;
    constructor() {
        this.dataRef = new Firebase('https://boiling-inferno-1117.firebaseio.com/votes');
    }
}

boot.ts

bootstrap(App, [HTTP_PROVIDERS, 
    ROUTER_PROVIDERS, 
    IdentityService, 
    ApiService, 
    UrlService, 
    provide(FirebaseService, {useValue: new FirebaseService()})
])
.catch(err => console.error(err));

app.ts

@Component({
    selector: 'app',
    templateUrl: 'app/app.html',
    directives: [AuthRouterOutlet, ROUTER_DIRECTIVES]
})
export class App {        
    constructor(private firebase:FirebaseService)
    {

    }        
}

Systemjs config:

System.config({
    defaultJSExtensions: true,
    map: {
        'jquery': 'vendors/js/jquery',
        'firebase': 'vendors/js/firebase'
    },
    packages: {
        angular2: { defaultExtension: false },
        rxjs: { defaultExtension: false }
    }
});

and importing Firebase like below:

import * as Firebase from 'firebase';

Any suggestion on what I can do to resolve this situation is greatly appreciated.

3
  • Can we assume that this: provide(FirebaseService, {useValue: new FirebaseService()}) is a typo? Because you should be using providers and it take an array afterward. providers : [FirebaseService] Commented Apr 24, 2016 at 14:35
  • Could you also provide your SystemJS configuration? And the way you import the Firebase class? Thanks! Commented Apr 24, 2016 at 14:36
  • @R.Richards Tried that also. Same error. Commented Apr 24, 2016 at 15:17

1 Answer 1

1

I would configure Firebase this way:

System.config({
  map: {
    firebase: '/node_modules/firebase/lib/firebase-web.js',
    (...)
  },
  (...)
});

and import it like this in the TypeScript file:

import * as Firebase from 'firebase';

See the doc of Angularfire2 for more details:

Sign up to request clarification or add additional context in comments.

3 Comments

I have a gulp task that copies the firebase-web.js file from node_modules folder into my dist/vendors/js directory. So it seems its essentially the same as what you showed. Am I wrong?
In fact you specify the folder not the js file into your SystemJS configuration ;-) You need to map the js file.
vendors/js/firebase here firebase is the file name like firebase.js. Since, I have defaultJSExtensions: true enabled .js seems to be appended for me. That is why jquery from my config works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.