Skip to content

Instantly share code, notes, and snippets.

@brenopolanski
Created April 30, 2021 14:37
Show Gist options
  • Save brenopolanski/bd507800549afe5b26754e3d70a5be8d to your computer and use it in GitHub Desktop.
Save brenopolanski/bd507800549afe5b26754e3d70a5be8d to your computer and use it in GitHub Desktop.
firebase x nextjs ReferenceError: navigator is not defined
> Build error occurred
ReferenceError: navigator is not defined
    at Object.areCookiesEnabled (/vercel/28f5c7d0/node_modules/@firebase/util/dist/index.node.cjs.js:650:5)
if (!firebase.apps.length) {
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
}

// change to:
if (typeof window !== 'undefined' && !firebase.apps.length) {
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
}
@Noor-Khan
Copy link

This doesn't work for me

@AbdullahAlhariri
Copy link

doesn't work

@fs-doc
Copy link

fs-doc commented Nov 25, 2022

This happens when you try to initialize the firebase app server side, where the window object is not available.

In order to solve this, initialize the app inside useEffect hook (this way window object is available to firebase method):

useEffect(() =>{
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        const messaging = getMessaging(app);
 })

@amitvishwa19
Copy link

amitvishwa19 commented Jan 6, 2024

This happens when you try to initialize the firebase app server side, where the window object is not available.

In order to solve this, initialize the app inside useEffect hook (this way window object is available to firebase method):

useEffect(() =>{
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        const messaging = getMessaging(app);
 })

Great, its work, thankx a ton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment