Socket
Socket
Sign inDemoInstall

eslint-plugin-no-typeof-window-undefined

Package Overview
Dependencies
99
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eslint-plugin-no-typeof-window-undefined

ESLint rule to avoid typeof window === 'undefined' for SSR check


Version published
Weekly downloads
13K
decreased by-5.39%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

eslint-plugin-no-typeof-window-undefined

ESLint rule that helps avoid typeof window === "undefined" check and fix it to typeof document === "undefined" instead. It's recommended to use document over window because server runtimes like Deno have a global window available.

npm version

Example

Installation

pnpm add -D eslint eslint-plugin-no-typeof-window-undefined

Usage

Add to .eslintrc

{
  "extends": ["plugin:no-typeof-window-undefined/recommended"]
}

Rules

✅ Set in the recommended configuration 🔧 Automatically fixable by the --fix CLI option.\

NameDescription💼🔧
no-typeof-window-undefinedImprove SSR/Browser environment check by using typeof document !== "undefined".🔧

no-typeof-window-undefined

Because the same JavaScript code can run in the browser as well as the server, sometimes you need to have a part of your code that only runs in one context or the other:

if (typeof window === "undefined") {
  // running in a server environment
} else {
  // running in a browser environment
}

This works fine in a Node.js environment, however, Deno actually supports window! So if you really want to check whether you're running in the browser, it's better to check for document instead:

if (typeof document === "undefined") {
  // running in a server environment
} else {
  // running in a browser environment
}

This will work for all JS environments (Node.js, Deno, Workers, etc.).

Credits

Keywords

FAQs

Last updated on 02 Dec 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc