question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error: firebase.initializeApp is not a function

See original GitHub issue

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”

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:21
  • Comments:24 (4 by maintainers)

github_iconTop GitHub Comments

33reactions
quantuminformationcommented, Dec 23, 2019

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

7reactions
nicolasgarniercommented, May 15, 2018

@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 https://github.com/webpack/webpack/issues/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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

firebase.initializeApp is not a function - Stack Overflow
Been having a similar issue with firebase v4.12.1 throwing error of initializeApp not being a function. I solved it by switching to using...
Read more >
firebase.initializeApp is not a function - Vue Forum
This import loads the firebase namespace. import firebase from 'firebase/app'; // These imports load individual services into the firebase ...
Read more >
firebase | JavaScript SDK | Node.js (client) API reference
Reference for firebase. ... appCheck; initializeApp; onLog; registerVersion; setLogLevel ... errors are logged, but debug and verbose logs are not).
Read more >
How to use the firebase-admin.initializeApp function in ... - Snyk
initializeApp (functions.config().firebase); const db = admin.database(); ... throw new Error(`Required option "${req_opt}" for LoggerFirestore not set.
Read more >
_firebase2.default.initializeApp is not a function - Google Groups
Your first import looks wrong. Try using: import * as firebase from 'firebase';. instead of just:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found