firebase.auth is not a function

前端 未结 18 1021
孤城傲影
孤城傲影 2020-11-28 10:30

I am using Webpack with firebase and firebase-admin.

To install firebase I ran

npm install --save firebase

I am importing firebase

18条回答
  •  感动是毒
    2020-11-28 10:39

    I know you've sorted your problem but there's no real answer to the original problem. The problem wasn't with the node_modules, it was with the way that you were importing the component.

    When you export a component ES6 way you normally do export default () => { console.log('default component export'); };

    default is the keyword here, when you import a component ES6 way like import firebase from 'firebase' it's grabbing the default property from the exported object.

    Keeping in mind the above example, here's what you've done wrong.

    Using ES6:

    import * as firebase from 'firebase'
    
    console.log(firebase.auth) // Undefined
    console.log(firebase.default.auth) // Function
    

    Using ES5:

    var firebase = require('firebase')
    
    console.log(firebase.auth) // Undefined
    console.log(firebase.default.auth) // Function
    

    Note the .default

    Hope this helps to explain what was wrong in the first place.

提交回复
热议问题