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

error: firebase.initializeApp is not a function #392

Closed
ee92 opened this issue May 11, 2018 · 24 comments · Fixed by #398
Closed

error: firebase.initializeApp is not a function #392

ee92 opened this issue May 11, 2018 · 24 comments · Fixed by #398

Comments

@ee92
Copy link

ee92 commented May 11, 2018

I'm trying to use the firebaseUI library for authentication in a react app made with create-react-app. It works great in a standard html/js app but I can't get it to work with react.

here is my simple login component:

import React, { Component } from 'react'
import * as firebase from 'firebase'
import firebaseui from 'firebaseui'

const dbConfig = {
  apiKey: ...,
  authDomain: ...,
  ...
}
firebase.initializeApp(dbConfig)

const uiConfig = {
  signInFlow: 'popup',
  signInOptions: [
    firebase.auth.GoogleAuthProvider.PROVIDER_ID,
    firebase.auth.EmailAuthProvider.PROVIDER_ID
  ]
}

class Login extends Component {

  componentDidMount() {
    console.log("component mounted")
    var ui = new firebaseui.auth.AuthUI(firebase.auth())
    ui.start('#firebaseui-auth-container', uiConfig)
  }

  render() {
    return (
      <div id="firebaseui-auth-container"></div>
    )
  }
}

export default Login

All the firebase stuff works until I try to initialize the firebaseUI widget using new firebaseui.auth.AuthUI(firebase.auth()) which is when it throws an error saying firebase.initializeApp is not a function. Problem seems to be stemming from node_modules/firebaseui/dist/npm.js:340 where initializeApp gets called.

screen shot 2018-05-11 at 1 07 13 pm

Anyone else experienced this behavior (and hopefully resolved it)?

Here are my firebase dependancies btw:

"firebase": "^5.0.2",
"firebaseui": "^3.0.0"

@julientregoat
Copy link

having exactly the same error - can create the instance of firebase, but can't pass it to the firebase ui component. seems like people previously had this issue but their solutions didn't apply to my case

@julientregoat
Copy link

I didn't see these React firebase components before, but they're working for me instead of using this method

https://github.com/firebase/firebaseui-web-react

@bojeil-google
Copy link
Contributor

This is an issue with firebase-js-sdk. I have encountered issues with it in 5.0.2 which worked after switching back to 5.0.1.

@ee92
Copy link
Author

ee92 commented May 11, 2018

I'm getting signInWithCredential failed: First argument "credential" must be a valid credential. when I use the react-firebaseui component.

Which version on firebase are you using?

** 5.0.1 is working for with firebaseui but giving same 'credential' problem as the react-firebaseui component.
*** 4.13.0 works with both thanks @julientregoat @bojeil-google

@julientregoat
Copy link

@ee92 I was having that issue for a moment. I reinstalled firebase at 4.12.0 and now it appears to be working

@nicolasgarnier
Copy link
Contributor

Just released v3.0.4 of react-firebaseui that now depends on the latest major versions of firebase and firebaseui. I've restricted the peer dependency of firebase to v5.0.0 and v5.0.1 because of the webpack issue. I'll change that whenever the next minor version has the fix.

@nicolasgarnier
Copy link
Contributor

I wasn't able to reproduce the signInWithCredential failed: First argument "credential" must be a valid credential. issue you guys mentioned with v5.0.1 but if you are able to with the new build please file an issue on https://github.com/firebase/firebaseui-web-react

@ishiijp
Copy link

ishiijp commented May 15, 2018

I had the same error with nuxt.js.
I could solve this issue to add the following lines to webpack config

config.resolve.alias['firebase/app'] = __dirname + '/node_modules/firebase/app/dist/index.cjs.js'

Before adding these lines, webpack loads '~/node_modules/firebase/app/dist/index.esm.js'

@mnehpets
Copy link

mnehpets commented May 15, 2018

I've seen the same issue with the firebaseui widget giving the error Uncaught TypeError: firebase.initializeApp is not a function. As far as I can see, this is is a result of webpack's module semantics. Specifically, when firebaseui/dist/npm.js is loaded by webpack, the module import statement firebase=require('firebase/app') pulls in the es2015 module firebase/app/dist/index.esm.js.

Since firebase/app/dist/index.esm.js exports firebase using export default, webpack's semantics expects clients to access the firebase object by require('firebase/app').default.

I don't know what the right solution here is, but I suspect doing something like wrapping the two require calls in firebaseui/dist/npm.js in the usual interopDefault might work. This needs to be done in firebaseui-web/gulpfile.js.

For the moment, I'm working around this issue by loading firebase/app/dist/index.esm.js with babel rather than webpack, and using babel's add-module-exports plugin. I've put together a hacky demonstration of this issue and the work-around in https://github.com/mnehpets/firebaseui-webpack. In that code, the workaround is disabled by default, so opening dist/login.html results in Uncaught TypeError: firebase.initializeApp is not a function. If you enable the babel loader in lines 19-21 of webpack.conf.js, the login widget works once again.

@nicolasgarnier
Copy link
Contributor

@jshcrowthe FYI.

I think the issues is coming from the mixed use of both the ESM and CJS imports of the firebase library.

In react projects for instance, many developers will use ESM imports:

import firebase from 'firebase/app';

but, in the same project, they will import firebaseui which contains:

var firebase = require('firebase/app');

The issue here is that webpack will only load firebase once and because ESM was used in the project it will load @firebase/app/dist/index.esm.js which is has a slightly different API than index.cjs.js (the difference is that index.esm.js has the SDK under the default attribute).

One possible workaround for firebaseui is to fallback to .default if available directly in npm.js. I've sent a PR for this in #398

Alternatively developers can add the following to their project's webpack config:

      {/* Workaround for issue https://github.com/firebase/firebaseui-web/issues/392 */},
      {
        test: /npm\.js$/,
        loader: 'string-replace-loader',
        include: path.resolve('node_modules/firebaseui/dist'),
        query: {
          search: 'require(\'firebase/app\');',
          replace: 'require(\'firebase/app\').default;',
        },
      },

Other way to fix this for everyone would be to have a default self reference to the firebase SDK in index.cjs.js kinda exports.default = exports;

Not sure if there is a much better way to handle this. In webpack/webpack#6584 @sokra says both CJS and ESM should export the same API. Although it's nicer to use directly the way firebase has done it but it's true that it complicates things for webpack (it's not efficient to import the same library twice), we won't avoid mixed uses of require and import.

@invernizzi
Copy link

Thanks for the solution, Nicolas!
Small fix for the webpack workaround:

      {
        test: /npm\.js$/,
        loader: 'string-replace-loader',
        include: path.resolve('node_modules/firebaseui/dist'),
        options: {
          search: 'require(\'firebase/app\');',
          replace: 'require(\'firebase/app\').default;',
        },
      },

@shawnmitchell
Copy link

Very cool, but CRA

bojeil-google pushed a commit that referenced this issue May 15, 2018
)

* Using default attribute of Firebase require statement if available

fixes #392

* Add typeof
@mdrideout
Copy link

@invernizzi 's webpack code worked for me. Just needed to remember to install "string-replace-loader".

@potatowave
Copy link

potatowave commented May 16, 2018

To those who got this work, was the fix placing this in webpack.config.js as so?

module.exports = {
        test: /npm\.js$/,
        loader: 'string-replace-loader',
        include: path.resolve('node_modules/firebaseui/dist'),
        options: {
          search: 'require(\'firebase/app\');',
          replace: 'require(\'firebase/app\').default;',
     },
},

sne11ius added a commit to sne11ius/egghead.space that referenced this issue May 18, 2018
@sprzedwojski
Copy link

Any solution for a create-react-app project where I cannot modify the webpack config?
For now I've downgraded firebase to 4.12.0 and react-firebaseui to 1.2.3 and it works, but it would be best to use the latest versions in the long run.

@taykcrane
Copy link

If it helps others, for me, this issue was solved by removing firebase-tools from my package.json file.

@quantuminformation
Copy link

Getting same issue, Anyone got a solution for rollup here?

https://github.com/QuantumInformation/svelte-fullstack-starter/blob/firebase/src/stores/firebaseUserStore.js#L21

Strange to have this problems just with a simple import.

@quantuminformation
Copy link

Turns out I had to do this instead:
firebase.default.initializeApp(firebaseConfig);

@tensegrity666
Copy link

Turns out I had to do this instead:
firebase.default.initializeApp(firebaseConfig);

thnx, it works

@quantuminformation
Copy link

I wish I didn't have to write it like that tbh

@sgup
Copy link

sgup commented Nov 30, 2020

This works for me:

import firebase from "firebase-admin";
firebase.initializeApp({...})

@sumj25
Copy link

sumj25 commented Dec 16, 2020

Turns out I had to do this instead:
firebase.default.initializeApp(firebaseConfig);

thanx man it works

@Otien0
Copy link

Otien0 commented Dec 21, 2020

Turns out I had to do this instead:
firebase.default.initializeApp(firebaseConfig);

Thank you, it works now!

@izzo2k4
Copy link

izzo2k4 commented Feb 22, 2021

Turns out I had to do this instead:
firebase.default.initializeApp(firebaseConfig);

Like a charm! Thank you.

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

Successfully merging a pull request may close this issue.