Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ReferenceError: window is not defined, Firebase analytics

I get the error ReferenceError: window is not defined when trying to initialize Firebase analytics in my Next.js project.

Here’s the relevant code snippet:

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
let analytics;
if (window !== undefined) {
   analytics = getAnalytics(app);
}

onAuthStateChanged(auth, (user) => {
  if (user) {
    // User signed in
    const uid = user.uid;
    console.log(uid);
  } else {
    // User signed out.
  }
});

export { app, auth, analytics };

The issue was resolved in another post; but the proposed solution doesn’t seem to be working for me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If window doesn’t exist, you can’t check its type.

Consider:

if (typeof window !== 'undefined') {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading