Gatsby build error "TypeError: Cannot read property 'split' of null" related to firebase
See original GitHub issueDescription:
error message on gatsby build on Netlify: TypeError: Cannot read property ‘split’ of null
Steps to reproduce:
My code
`import React, {Component} from ‘react’;
import StyledFirebaseAuth from “react-firebaseui/StyledFirebaseAuth” import firebase from “firebase/app” import “firebase/auth”
firebase.initializeApp({ apiKey: “xxxxxx”, authDomain: “xxxxx”, databaseURL: “xxxxx”, projectId: “xxxx”, storageBucket: “”, messagingSenderId: “xxxxx”, appId: “xxxxx” })
class IndexPage extends Component { state = { isSignedIn : false, }
uiConfig = { signInFlow: ‘popup’, signInOptions: [ firebase.auth.GoogleAuthProvider.PROVIDER_ID, firebase.auth.EmailAuthProvider.PROVIDER_ID, ], callbacks: { signInSuccessWithAuthResult : () => false } }
componentDidMount = () => {
const app = import (‘firebase’)
if (typeof window !== undefined
) {
const app = import (‘firebase’)
firebase.auth().onAuthStateChanged(user => {
this.setState({ isSignedIn: !!user })
}) } }
render() { return( <div> {this.state.isSignedIn ? ( <div>Sign In !</div>) : (
< StyledFirebaseAuth
uiConfig = {this.uiConfig}
firebaseAuth={firebase.auth()}/>
)}
</div>
)
} }
export default IndexPage`
Error message on Netlify
If someone can explain me how to make this working, thanks !
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Anyone having their builds fail because they’re calling firebase functions in client, the tl;dr that wasn’t all that obvious from here is to check for whether window exists when defining firebase, as so:
Felt it worth adding to this issue as it’s mentioned a fair bit online.
If anyone coming over to this issue looking to fix this, I’ve got the build working with
react-firebase-hooks
andgatsby-plugin-firebase
by replacing useAuthState fromreact-firebase-hooks
with a custom hook as suggested here:https://gist.github.com/jeffreymeng/78bd7f6b0f301fa5ef04359cd512222b
So if you have created the hook as in the link above, the next step would be to change the implementation as following: