How to import Firebase SDK modules separately in Kotlin/JS?

Hi,

I’m trying to import the Firebase SDK in a Kotlin/JS project but I can’t find a way to import each module separately so that the whole SDK is not loaded when I only need a couple of modules from it.

I’m importing the library from Gradle like this:

implementation(npm("firebase", "7.13.2"))

and I’m using the Kotlin wrappers for it available in the following project: https://github.com/lamba92/firebase-multiplatform which seem to have been generated by Dukat and then improved by @lamba92.

When I run the app I get the following warning:

It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.

For the module builds, these are available in the following manner
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):

CommonJS Modules:
const firebase = require('firebase/app');
require('firebase/<PACKAGE>');

ES Modules:
import firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

Typescript:
import * as firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

I have tried to change the Kotlin wrappers to only import each module like this:

@file:JsModule("firebase/app")
@file:JsModule("firebase/analytics")
...

and I can call firebase.initializeApp() which works fine, but when I try to access features like analytics like this:

val firebaseApp = firebase.initializeApp(...)

firebaseApp.analytics().logEvent("test")

then I get the following error in the console at runtime:

Uncaught TypeError: firebaseApp.analytics is not a function

In this case, when I check the app.js file generated in the build folder I can see that the only require() statement is for firebase/app for some reason.

Has anyone come across this kind of issue with Firebase or any other library that has to be imported this way?

Happens because I still need to implement the many packages, until then when importing the whole SDK is the only reasonable way. ATM I am very busy with university and I do not have time :confused: Any help on the repo is welcome tho!

Most likely an error in the externals I’ll have a look as soon as possible. Could you open an issue here?

Thanks for the answer @lamba92. I’m actually not using your library, I’m just using the Kotlin wrapper of the Firebase JS library that are inside your repo, the ones created by Dukat (I downloaded them and added them to my project). So the issue is not on your library.

Do you have any idea of how to import certain modules from the Firebase JS SDK even if you haven’t implemented it yet?

You need to declare those functions inside a file with the right @JSModule() annotation!

@lamba92 that’s what I tried as I explained on my first post, e.g., @file:JsModule("firebase/analytics") but they don’t seem to work as I get the error Uncaught TypeError: firebaseApp.analytics is not a function.

Have you tried it before and it worked?

Sorry I misread, I will have a look tomorrow morning!

Thanks @lamba92 :slight_smile: