Skip to content

Instantly share code, notes, and snippets.

@gtchakama
Last active February 10, 2023 23:39
Show Gist options
  • Save gtchakama/fc8055af6b08ab3d7cc8ab4835932b1e to your computer and use it in GitHub Desktop.
Save gtchakama/fc8055af6b08ab3d7cc8ab4835932b1e to your computer and use it in GitHub Desktop.
Fix : ReferenceError: window is not defined in JavaScript
if (typeof window !== 'undefined') {
console.log('You are on the browser');
// ✅ Can use window here
console.log(window.innerWidth);
window.addEventListener('mousemove', () => {
console.log('Mouse moved');
});
} else {
console.log('You are on the server');
// ⛔️ Don't use window here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment