JavaScript Library Detector

6.7.0 · active · verified Sun Apr 19

js-library-detector is an npm package that provides programmatic access to the detection logic used by the 'Library Detector for Chrome' browser extension. It identifies various JavaScript frameworks and libraries in use on a given webpage by inspecting the `window` object and other global properties. The current stable version is 6.7.0, and the project demonstrates an active release cadence with frequent updates adding new library detections (e.g., WordPress, Gatsby, Shopify, Wix, Magento) and refining existing ones. Its key differentiator is its comprehensive list of supported libraries and its origin as a widely used browser extension, providing robust, battle-tested detection capabilities in a headless or server-side rendering context via tools like JSDOM or Puppeteer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use js-library-detector in a Node.js environment by simulating a browser window with JSDOM, loading HTML content, and then executing the detection logic to identify JavaScript libraries present on the page.

import { JSDOM } from 'jsdom';
import { detect } from 'js-library-detector';

async function detectLibrariesOnPage(url) {
  try {
    const response = await fetch(url);
    const html = await response.text();

    const dom = new JSDOM(html, {
      url: url,
      resources: 'usable',
      runScripts: 'dangerously',
      beforeParse(window) {
        // Mock common browser APIs if necessary for detections that rely on them
        window.Element.prototype.getBoundingClientRect = () => ({
          width: 10, height: 10, top: 0, left: 0, bottom: 0, right: 0,
        });
      }
    });

    // Wait for the DOM and scripts to load/execute
    // This is crucial as many libraries are initialized after page load
    await new Promise(resolve => {
      dom.window.addEventListener('load', resolve);
      // Fallback in case 'load' doesn't fire for some reason (e.g., no scripts)
      setTimeout(resolve, 5000); // Wait up to 5 seconds for scripts
    });

    // Perform detection using the JSDOM window object
    // The detect function expects a window object as 'this' context
    const detected = detect.call(dom.window, { html: dom.window.document.documentElement.outerHTML });

    const detectedNames = Object.keys(detected).filter(lib => detected[lib].found);
    console.log(`Detected libraries on ${url}:`);
    if (detectedNames.length > 0) {
      detectedNames.forEach(name => {
        console.log(`- ${name} (version: ${detected[name].version || 'N/A'})`);
      });
    } else {
      console.log('No known libraries detected.');
    }
  } catch (error) {
    console.error(`Error detecting libraries on ${url}:`, error);
  }
}

// Example usage: detect libraries on a public website (e.g., Google)
// Note: This requires 'jsdom' and 'node-fetch' to be installed.
// `npm install jsdom node-fetch`
detectLibrariesOnPage('https://www.google.com/');

view raw JSON →