{"id":14730,"library":"node-contains","title":"Node.prototype.contains() Polyfill","description":"The `node-contains` package, currently at version 1.0.0 and last published in September 2014, was designed to provide a cross-browser polyfill for the `Node.prototype.contains()` DOM method. Its original purpose was to ensure consistent behavior across older web browsers that lacked native support for this functionality. However, this method has been natively supported by all major web browsers since July 2015. Consequently, this package is now largely obsolete and considered abandoned for modern web development. While it might still be encountered in projects targeting extremely legacy environments, for any contemporary application, including `node-contains` introduces unnecessary overhead and potential compatibility issues. It does not follow a regular release cadence and offers no unique differentiators over native browser implementations.","status":"abandoned","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/webmodules/node-contains","tags":["javascript","browser","node","contains","polyfill","dom"],"install":[{"cmd":"npm install node-contains","lang":"bash","label":"npm"},{"cmd":"yarn add node-contains","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-contains","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a polyfill that directly modifies `Node.prototype`. There are no named exports; the import is for its side-effects.","wrong":"import { contains } from 'node-contains';","symbol":"Node.prototype.contains","correct":"import 'node-contains';"},{"note":"For CommonJS environments, requiring the module applies the polyfill as a side-effect. It does not return an export.","wrong":"const contains = require('node-contains');","symbol":"Node.prototype.contains (CommonJS)","correct":"require('node-contains');"}],"quickstart":{"code":"import 'node-contains'; // Apply the polyfill (though likely unnecessary in modern browsers)\n\nfunction checkContains() {\n  const parentDiv = document.createElement('div');\n  parentDiv.id = 'parent';\n  const childSpan = document.createElement('span');\n  childSpan.id = 'child';\n  parentDiv.appendChild(childSpan);\n\n  document.body.appendChild(parentDiv);\n\n  // Using Node.prototype.contains() after the polyfill (or native support)\n  const isContained = parentDiv.contains(childSpan);\n  const selfContained = parentDiv.contains(parentDiv); // Node is considered to contain itself\n  const notContained = document.body.contains(document.createElement('p'));\n\n  console.log('Is childSpan contained in parentDiv?', isContained); // Expected: true\n  console.log('Is parentDiv contained in itself?', selfContained); // Expected: true\n  console.log('Is new P not contained in body?', notContained); // Expected: false (unless appended)\n\n  document.body.removeChild(parentDiv);\n}\n\n// Run the check when the DOM is ready\nif (document.readyState === 'loading') {\n  document.addEventListener('DOMContentLoaded', checkContains);\n} else {\n  checkContains();\n}","lang":"javascript","description":"This quickstart demonstrates how to include the `node-contains` polyfill and then use the `Node.prototype.contains()` method to check if a DOM node is a descendant of another."},"warnings":[{"fix":"Remove `node-contains` from your project's dependencies and codebase. Rely on the native browser implementation of `Node.prototype.contains()`.","message":"This polyfill is effectively obsolete. The `Node.prototype.contains()` method has been natively supported by all major web browsers since July 2015, making this package redundant for nearly all modern web development environments. Including it introduces unnecessary bundle size and may lead to subtle compatibility issues or conflicts if other polyfills or libraries also modify `Node.prototype`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Migrate away from this package. If you absolutely need `Node.prototype.contains` in an ancient browser environment not covered by native support, consider a more modern, maintained polyfill or implement a minimal custom solution after careful consideration of risks.","message":"The package is unmaintained, with its last update in September 2014. Using unmaintained dependencies can expose projects to security vulnerabilities (e.g., supply chain attacks, unpatched bugs) as no further patches or updates are released for discovered issues.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Audit your dependency tree for other polyfills or libraries that might target `Node.prototype.contains`. Prioritize native browser functionality, and if polyfills are strictly necessary, ensure they are well-maintained and don't create conflicts. Use conditional polyfilling techniques if possible.","message":"As a polyfill, `node-contains` modifies the global `Node.prototype`. While this is its intended function, in complex applications or environments where multiple polyfills or libraries might attempt to patch or define the same method, this could lead to unexpected behavior, overwrites, or conflicts, making debugging difficult.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"This package is explicitly for client-side browser environments. Ensure it is only included in your client-side bundles and excluded from server-side Node.js code. Use appropriate build tool configurations (e.g., Webpack targets, browser field in package.json) to achieve this separation.","cause":"Attempting to include or execute this browser-specific polyfill in a non-browser (e.g., Node.js server-side) environment.","error":"ReferenceError: Node is not defined"},{"fix":"Verify that the `element` variable is indeed a DOM `Node` (e.g., obtained via `document.getElementById()`). If targeting extremely old browsers, ensure `import 'node-contains';` (or `require('node-contains');`) is executed at the very beginning of your application's entry point before any DOM manipulation code that relies on `contains`.","cause":"This error typically occurs when `Node.prototype.contains` is invoked on an object that is not a valid DOM `Node` or if the polyfill (or native support) has not been loaded/executed before the method call in a very old browser context. In modern browsers, this indicates `element` is not a `Node`.","error":"TypeError: undefined is not a function (evaluating 'element.contains(otherElement)')"},{"fix":"Install the package using your package manager: `npm install node-contains` or `yarn add node-contains`. Then, double-check the import or require statement for any spelling errors and ensure it matches the package name exactly.","cause":"The `node-contains` package has not been installed as a dependency in your project, or there is a typo in the import/require statement or package name.","error":"Error: Module not found: Error: Can't resolve 'node-contains'"}],"ecosystem":"npm"}