This package provides a very minimalist utility to determine if the current JavaScript environment has access to the Document Object Model (DOM), primarily used for writing isomorphic (universal) JavaScript applications that need to behave differently client-side versus server-side. It exports a simple boolean value, `true` if `window`, `window.document`, and `window.document.createElement` are all defined, and `false` otherwise. The package is extremely lightweight with no dependencies. First released as version `0.1.0` and last updated over nine years ago, its development is considered abandoned. Due to its age, it primarily uses CommonJS module syntax, meaning direct native ESM imports may not function without a bundler or transpilation layer. Modern alternatives often include more sophisticated environment detection or rely on frameworks to abstract these differences. Its main differentiator is its absolute simplicity and singular focus, though its lack of maintenance makes it a less reliable choice for new projects.
npm install can-use-domVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to import and use the `can-use-dom` boolean to conditionally execute browser-specific code.
Always use `const canUseDOM = require('can-use-dom');` for direct usage. For projects targeting modern ESM, ensure your bundler (e.g., Webpack, Rollup, Vite) is configured to handle CommonJS modules.For new projects, consider more actively maintained alternatives or implement your own robust environment detection tailored to your specific needs. For existing projects, be aware of potential compatibility issues with newer environments.
If running in a mocked DOM environment, you might need additional checks to distinguish it from a real browser. For more granular control, extend the check with specific API detections or rely on framework-level environment variables.
Change the import statement to `const canUseDOM = require('can-use-dom');`.Ensure all browser-specific code paths are strictly guarded by `if (canUseDOM) { ... }` or equivalent environment checks, especially when developing isomorphic applications.No dependency data recorded yet.