ESLint Plugin: no-typeof-window-undefined

raw JSON →
0.0.2 verified Sat Apr 25 auth: no javascript

ESLint rule (version 0.0.2) that flags typeof window === 'undefined' patterns used for SSR checks. Encourages using a helper like `import { isBrowser } from 'some-ssr-helper'` for cleaner and more maintainable code. No updates since initial release. Minimal, single-rule plugin with peer dependency on ESLint ^4-^8.

error ESLint couldn't find the plugin "eslint-plugin-no-typeof-window-undefined"
cause Plugin not installed or not added to plugins array.
fix
npm install --save-dev eslint-plugin-no-typeof-window-undefined and add to plugins: ['no-typeof-window-undefined']
error Definition for rule 'no-typeof-window-undefined' was not found
cause Rule name used without plugin prefix in rules config.
fix
Use 'no-typeof-window-undefined/no-typeof-window-undefined': 'error'
gotcha Rule name is 'no-typeof-window-undefined/no-typeof-window-undefined' – you must prefix with the plugin namespace.
fix Use 'no-typeof-window-undefined/no-typeof-window-undefined': 'error' in rules.
gotcha Does not support ESLint flat config (ESLint 9+). No updated version.
fix Manually adapt plugin to flat config format or use legacy eslintrc.
npm install eslint-plugin-no-typeof-window-undefined
yarn add eslint-plugin-no-typeof-window-undefined
pnpm add eslint-plugin-no-typeof-window-undefined

Shows how to install and configure the ESLint plugin to flag typeof window === 'undefined' and suggests using a helper function instead.

// .eslintrc.js (ESLint <=8)
module.exports = {
  plugins: ['no-typeof-window-undefined'],
  rules: {
    'no-typeof-window-undefined/no-typeof-window-undefined': 'error'
  }
};

// Example of flagged code:
if (typeof window === 'undefined') { /* server code */ }

// Suggested fix:
import { isBrowser } from 'some-helper';
if (!isBrowser) { /* server code */ }