{"id":10610,"library":"capability","title":"JavaScript Environment Capability Detection","description":"capability.js is a lightweight JavaScript library designed for detecting specific environmental capabilities, primarily focusing on browser features. Released in 2016, its current stable version is 0.2.5, indicating it has not seen significant development since its initial release. The library allows developers to define custom capability tests using a simple API and then query or enforce these capabilities. Unlike comprehensive feature detection libraries like Modernizr, `capability.js` provides a minimalist approach for defining and checking individual features such as `Object.create`, `Array.prototype.forEach`, or `Function.prototype.bind`. Its release cadence appears to be inactive, with no recent updates. It relies on CommonJS modules and requires a bundler like Webpack or Browserify for use in browser environments, as native ES modules were not widely supported when it was developed. This makes it less suitable for modern projects that typically use native ES modules and rely on built-in browser feature detection or more actively maintained polyfill solutions.","status":"abandoned","version":"0.2.5","language":"javascript","source_language":"en","source_url":"https://github.com/inf3rno/capability","tags":["javascript","environment","browser","capability","test","detect"],"install":[{"cmd":"npm install capability","lang":"bash","label":"npm"},{"cmd":"yarn add capability","lang":"bash","label":"yarn"},{"cmd":"pnpm add capability","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses CommonJS `require()` syntax as it was released in 2016. Direct ESM `import` is not supported without a CommonJS-to-ESM transpilation step.","wrong":"import capability from 'capability';","symbol":"capability","correct":"const capability = require('capability');"},{"note":"Used for defining new capabilities. Access through the main `capability` object.","symbol":"capability.define","correct":"const capability = require('capability');\ncapability.define('myFeature', () => /* test logic */);"},{"note":"The `test` function is a method of the main `capability` object, not a named export. It can also be invoked as `capability(name)` for brevity.","wrong":"import { test } from 'capability';","symbol":"capability.test","correct":"const capability = require('capability');\nconst isSupported = capability.test('Object.create');"},{"note":"Specific capability groups (like ES5 features) can be 'required' as sub-modules to automatically run their `check` functions, throwing an error if requirements are not met.","wrong":"import { es5 } from 'capability';","symbol":"es5","correct":"require('capability/es5');"}],"quickstart":{"code":"const capability = require('capability');\n\n// Define a custom capability\ncapability.define('XMLHttpRequest', function () {\n  return typeof XMLHttpRequest !== 'undefined';\n});\n\n// Test if a capability is supported\nconst hasObjectCreate = capability.test('Object.create');\nconsole.log(`Object.create is supported: ${hasObjectCreate}`);\n\nconst hasCustomFeature = capability('XMLHttpRequest'); // Shorthand for capability.test\nconsole.log(`XMLHttpRequest is supported: ${hasCustomFeature}`);\n\n// Enforce a capability (will throw an error if not supported)\ntry {\n  capability.check('Array.prototype.forEach');\n  console.log('Array.prototype.forEach is available.');\n} catch (error) {\n  console.error(`Error: ${error.message}`);\n}\n\n// Load a specific capability group to enforce its requirements\ntry {\n  require('capability/es5'); // This will run checks for all ES5 capabilities defined in the library\n  console.log('Environment meets ES5 requirements (via /es5 module).');\n} catch (error) {\n  console.error(`Error loading ES5 capabilities: ${error.message}`);\n}","lang":"javascript","description":"Demonstrates how to define, test, and enforce capabilities, including using the shorthand `capability()` and loading specific capability modules."},"warnings":[{"fix":"Consider using native browser APIs for feature detection (e.g., `if (typeof window.fetch === 'function')`), or a modern polyfill solution combined with a bundler.","message":"This library was last updated in 2016 and is likely unmaintained. Use in new projects is not recommended, and direct feature detection or actively maintained polyfill libraries are generally preferred for modern web development.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure your project uses CommonJS `require()` for this package, or configure your build tool (e.g., Webpack, Rollup, Parcel) to correctly bundle CommonJS modules.","message":"The library primarily uses CommonJS `require()` syntax. Attempting to use ES module `import` syntax directly will fail in most environments without a build step that handles CommonJS modules.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Integrate the package into a JavaScript bundling workflow (e.g., Webpack, Rollup) to ensure it's correctly loaded in a browser environment.","message":"For browser usage, the library explicitly states it requires a 'node module loader' like Browserify or Webpack. It is not designed for direct inclusion via `<script>` tags without a bundling step.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `const capability = require('capability');` instead of `import capability from 'capability';` or ensure your build tool correctly processes CommonJS modules for browser/ESM environments.","cause":"Attempting to `import` 'capability' in a CommonJS-only Node.js environment or without proper bundler configuration for CommonJS modules.","error":"Error: Cannot find module 'capability'"},{"fix":"Ensure you are importing the entire module as a default object: `const capability = require('capability');` and then calling `capability.test('feature');`.","cause":"Incorrectly importing the `capability` object, such as attempting a named import `import { test } from 'capability'` when `test` is a method of the default export.","error":"TypeError: capability.test is not a function"}],"ecosystem":"npm"}