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.
CodePudding user response:
If window
doesn't exist, you can't check its type.
Consider:
if (typeof window !== 'undefined') {