Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firebase.auth is not a function #107

Closed
kierangilliam opened this issue Jul 15, 2020 · 5 comments
Closed

firebase.auth is not a function #107

kierangilliam opened this issue Jul 15, 2020 · 5 comments

Comments

@kierangilliam
Copy link

I'm using Firebase in a project. I believe there is some issue when using nollup and the firebase imports: import 'firebase/auth' and import 'firebase/analytics'. When I run the following with nollup I get the error:

Uncaught (in promise) TypeError: firebase.auth is not a function

<script lang='ts'>
    import { onMount } from 'svelte'
    import * as firebase from 'firebase/app'
    import 'firebase/auth'
    import 'firebase/analytics'
    import { Authorization } from '@state'
    import { authInitialized } from './utils'

    const config = {
        ...
    }

    onMount(async () => {
        firebase.initializeApp(config)
        $authInitialized = true

        firebase.auth().onAuthStateChanged(function(user) {
            firebase.analytics()
            let idToken 

            if (user) {
                idToken = user.getIdToken.toString()
            } 

            $Authorization = idToken ? `Bearer ${idToken}` : null
        });
    })
</script>

However, when I run using plain rollup, I do not get this error. Any ideas?

@PepsRyuu
Copy link
Owner

Can you provide a reproducible example? Just trying it out, and I'm getting the same result between Nollup and Rollup.

image

image

For the code to work, you should switch from namespace import to default import for firebase/app. It doesn't export any named properties at all.

import firebase from 'firebase/app';

@PepsRyuu
Copy link
Owner

Upon further digging, this seems to be some weird documentation / TypeScript problem with Firebase:

firebase/firebase-js-sdk#3315 (comment)
firebase/firebase-js-sdk#1832
firebase/firebase-js-sdk#1008 (comment)

Not entirely sure what's going on, but it sounds like their documentation assumes you're using TypeScript, and that TypeScript has a different behaviour for resolving namespace imports. I think TypeScript has a flag that allows the namespace import to work even though it's a default export. Very weird and goes against the ESM spec.

@kierangilliam
Copy link
Author

Here's a minimal example. Run yarn dev for normal rollup, and yarn dev:nollup for nollup.

Rollup will throw an error because there's no config, but it loads firebase correctly.

@PepsRyuu
Copy link
Owner

Running dev for Rollup doesn't show me anything but "Cannot GET /" :(

@PepsRyuu
Copy link
Owner

Closing as the original issue was with Firebase's documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants