JavaScript Library Detector
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
-
TypeError: Cannot read properties of undefined (reading 'window')
cause The `detect` function was called without a valid `window` object in its execution context, likely in a Node.js environment without JSDOM or similar setup.fixEnsure you are either running the detection in a browser environment, or providing a simulated browser `window` object when using it in Node.js, e.g., `detect.call(dom.window, options);`. -
ReferenceError: document is not defined
cause Similar to the `window` error, this occurs when the detection logic attempts to access `document` (which resides on `window`) in an environment where it's not present.fixSet up a JSDOM instance in your Node.js environment and pass its `window` object to the `detect` function's `this` context, or execute the code within a browser or headless browser context. -
Detected libraries are inconsistent or incomplete compared to actual page content.
cause This often happens when running detection on dynamically loaded content or pages that execute complex JavaScript after initial DOM load. The detector might run before all scripts have initialized the libraries.fixWhen using JSDOM or Puppeteer, ensure you wait sufficiently for the page's scripts to execute and for all dynamic content to render before invoking the `detect` function. In JSDOM, use `dom.window.addEventListener('load', ...)` or a `setTimeout` with a reasonable delay after `runScripts: 'dangerously'` is enabled. In Puppeteer, use `page.goto(url, { waitUntil: 'networkidle0' })` or `page.waitForSelector()`.
Warnings
- breaking Detection for 'Create React App' was removed due to conflicts and breaking certain login flows in v6.0.0. If your application relies on detecting CRA, this functionality is no longer available.
- gotcha The `detect` function must be called with a browser-like `window` object as its `this` context for accurate results. Running it directly in a Node.js environment without a simulated DOM (like JSDOM) or headless browser (like Puppeteer) will yield incorrect or no detections.
Install
-
npm install js-library-detector -
yarn add js-library-detector -
pnpm add js-library-detector
Imports
- detect
import detector from 'js-library-detector';
import { detect } from 'js-library-detector'; - detect (CommonJS)
const detector = require('js-library-detector'); detector();const { detect } = require('js-library-detector'); - libraries (CommonJS)
const { libraries } = require('js-library-detector');
Quickstart
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/');