Using third party libraries in Aurelia is incredibly easy and for Firebase it couldn’t be easier. You have two options when it comes to adding in Firebase to your Aurelia application.
Node module (prefered method)
There is an official Node module for Firebase which allows you to install and use it. All you have to do is run npm install firebase and then to use it within your Aurelia application add the following import.
import *as firebase from 'firebase';
If you are using TypeScript, the Firebase package ships with typings for using Firebase so you get intellisense for method calls and configuration options.
Script tag
The Firebase console when it provides you with the details of your application will actually provide a CDN script tag of the Firebase SDK. All you have to do is add it into your index.html file before anything else and a global firebase object will be made available to your app.
If you are using the CLI, you’ll want to add this into your index.html file above the bundle script so it gets included first.
This approach works, but it does make updating Firebase more involved because we have to replace the script tag in our HTML. The benefit of a Node module is you can update it (and downgrade) quite easily, as well as having the module within your app and not reliant on a network request.
Another downside of this approach is, if you’re using TypeScript you don’t get the intellisense out-of-the-box like you would with a Node module and import.
------- File not found or not accessible ------
| Location:C:/app/node_modules/firebase/app.js
| Requested by: C:/app/src/app.js
| Is this a package? Make sure that it is configured in aurelia.json and that it is not a Node.js package
It gives this error twice for each of these firebase files… firebase/auth.js firebase/database.js firebase/messaging.js firebase/storage
EDIT: I have tried getting firebase npm package to work with the CLI (webpack option) and also the CLI (“RequireJS”/gulp option) and cannot get either one working.
import * as firebase from 'firebase' is leaving the firebase variable as undefined every time…
I don’t know if this is the intended solution, and I would really like someone from aurelia to comment on this, since now I’m facing the same problem with rxjs and I will be damned if I’ll add ~50 module definitions.
I think, actually I am sure, this worked at one point. But then it stopped working and now I spent several evenings (this is a hobby project I’m trying to use it in), but no result.
I like aurelia, but if someone asks me about the aurelia-cli, I will say stay well away.
I’ll try to use webpack directly, I’ll post back about my experiences.
---- UPDATE
I finally got it all to work. I first did it with pure webpack and then I went back and regenerated the aurelia project with webpack. Yay! …I guess.
@TheFab have you been able to figure this out? I tried following your example for firebase and end up with a firebase is undefined error at runtime (though I no longer get errors when the aurelia cli bundles everything). Any additional guidance you can provide when using firebase with aurelia-cli @dwaynecharrington?
EDIT:
I migrated from aurelia-cli to webpack to get this working, but would still like to know how to handle this with aurelia-cli in the future.
I’ll work out how to get it working properly in Aurelia CLI and report back soon everyone. Worth noting that you can also include the CDN script inside of your index file which I have done and then access the global Firebase object in your application. It just means you don’t explicitly import anything and no Firebase modules get bundled in your app, which can be a benefit if your app is already huge, sometimes taking that additional script request can work out better than one large bundle.
What version of firebase and Aurelia did you use? I tried the same with firebase 4.8.2 and aurelia 1.1.5 but it didn’t work
(When I opened the application, I got some nasty error in console.log: Unhandled rejection Error: Mismatched anonymous define() module: function (require, exports, aurelia_framework_1)