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.
Common errors
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'
Warnings
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.
Install
npm install eslint-plugin-no-typeof-window-undefined yarn add eslint-plugin-no-typeof-window-undefined pnpm add eslint-plugin-no-typeof-window-undefined Imports
- default wrong
module.exports = { plugins: ['no-typeof-window-undefined'], rules: { 'no-typeof-window-undefined': 'error' } };correctmodule.exports = { plugins: ['no-typeof-window-undefined'], rules: { 'no-typeof-window-undefined/no-typeof-window-undefined': 'error' } }; - rules wrong
const { rules } = require('eslint-plugin-no-typeof-window-undefined');correctconst plugin = require('eslint-plugin-no-typeof-window-undefined'); // then use plugin.rules['no-typeof-window-undefined'] - flat config (ESLint 9+) wrong
import plugin from 'eslint-plugin-no-typeof-window-undefined'; export default [ { plugins: ['no-typeof-window-undefined'], ... } ];correctimport noTypeofWindowUndefined from 'eslint-plugin-no-typeof-window-undefined'; export default [ { plugins: { 'no-typeof-window-undefined': noTypeofWindowUndefined }, rules: { 'no-typeof-window-undefined/no-typeof-window-undefined': 'error' } } ];
Quickstart
// .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 */ }