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.

Gatsby build error "TypeError: Cannot read property 'split' of null" related to firebase

See original GitHub issue

Description:

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

Capture d’écran de 2019-07-29 13-56-03

If someone can explain me how to make this working, thanks !

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwithseismiccommented, Aug 10, 2020

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:

import firebase from 'gatsby-plugin-firebase'
const isBrowser = () => typeof window !== 'undefined'
const Firebase = isBrowser() ? firebase : {}

Felt it worth adding to this issue as it’s mentioned a fair bit online.

0reactions
eerrecaldecommented, Oct 23, 2020

If anyone coming over to this issue looking to fix this, I’ve got the build working with react-firebase-hooks and gatsby-plugin-firebase by replacing useAuthState from react-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:

import firebase from 'gatsby-plugin-firebase';
import { useAuthState } from 'react-firebase-hooks/auth';
...
const [user, userLoading] = useAuthState(firebase.auth());
import firebase from 'gatsby-plugin-firebase';
import useAuthState from '../hooks/use-auth-state'; // Changed
...
const [user, userLoading] = useAuthState(firebase); // Changed
Read more comments on GitHub >

github_iconTop Results From Across the Web

Gatsby: "Cannot read property 'split' of undefined" when ...
Now when I use Netlify to deploy, i'm getting this error which i'm not getting in Development mode. Cannot read property 'split' of...
Read more >
Cannot Read Property 'split' of Undefined
it will throw the TypeError: Cannot read property 'split' of undefined error. The split method. When split is called on a string, it...
Read more >
Cannot Read Property 'Split' Of Undefined When ...
it will throw the TypeError: Cannot read property 'split' of undefined error. The split method. When split is called on a string it...
Read more >
gatsby.jsのbuild 時の'TypeError: Cannot read property 'split' ...
今回の場合はwindow objectを使用しているわけではないのですが、Node.js環境でfirebase.auth()を呼び出していることが原因でした。firebaseのclient side ...
Read more >
Cannot read properties of undefined (reading 'length')
This error can be thrown for a lot of reasons, as it is incredibly common to reference the length property of string or...
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