error: firebase.initializeApp is not a function
See original GitHub issueI’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.
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:
- Created 5 years ago
- Reactions:21
- Comments:24 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Turns out I had to do this instead:
firebase.default.initializeApp(firebaseConfig);
@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:
but, in the same project, they will import
firebaseui
which contains: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 thanindex.cjs.js
(the difference is thatindex.esm.js
has the SDK under thedefault
attribute).One possible workaround for
firebaseui
is to fallback to.default
if available directly innpm.js
. I’ve sent a PR for this in #398Alternatively developers can add the following to their project’s webpack config:
Other way to fix this for everyone would be to have a
default
self reference to the firebase SDK inindex.cjs.js
kindaexports.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
andimport
.