# jsdom — card for AI agents

JavaScript implementation of web standards for testing DOM interactions.

Category: Testing · npm package `jsdom` · latest version 30.1.3 (checked 2026-11-01)
Install: `npm install jsdom`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## jsdom
- Create documents with `new JSDOM(html, options)` and work through `dom.window` / `dom.window.document`.
- Do not set `runScripts: "dangerously"` for user-supplied or Internet content. The sandbox is not foolproof. Use `runScripts: "outside-only"` with `window.eval` when you only need to run code from outside.
- Do not mash jsdom into Node globals (e.g. `global.window = dom.window`). Run DOM-dependent scripts and tests inside the jsdom environment.
- When loading subresources, set `resources: "usable"` and a real `url`. With the default `about:blank`, relative URLs fail to load. External scripts also need `runScripts: "dangerously"`.
- Do not rely on layout or rendering. Use `pretendToBeVisual: true` only when code needs `requestAnimationFrame` or a visible `document.visibilityState`.
- Set up `VirtualConsole` listeners before calling `new JSDOM()`, because errors can occur during parsing.
- Interceptors and dispatchers are ignored for synchronous `XMLHttpRequest` inside the page.

## What it is
- jsdom gives Node.js a browser-style DOM without a browser. The README describes it as an implementation of many web standards, notably the WHATWG DOM and HTML Standards. It states the goal plainly: "to emulate enough of a subset of a web browser to be useful for testing and scraping real-world web applications." The repository was created on 2010-01-19 and is MIT licensed.
- You pass a string of HTML to the `JSDOM` constructor and get back an object with a `window`. From there you use normal DOM APIs such as `document.querySelector`. A second options argument controls the rest: the page URL, the content type, whether scripts run, whether subresources load, and where console output goes.
- It is for code that needs a DOM but runs in Node, such as test suites and scrapers. The README is direct about the limits. jsdom does no layout or rendering, and the maintainers say its script sandbox is not foolproof.

## Caveats
- The sandbox is not a security boundary: The maintainers write that code inside a page's `<script>` elements can, if it tries hard enough, reach the Node.js environment and your machine. Use `runScripts: "dangerously"` only with code you trust.
- No layout or rendering: jsdom acts like a headless browser. `pretendToBeVisual: true` changes `document.hidden` and enables `requestAnimationFrame`. The README stresses that it still does not do any layout or rendering.
- Subresources are off, and the default URL breaks relative paths: Scripts, stylesheets, images and iframes do not load unless you set `resources: "usable"`. The default `url` is `"about:blank"`, so resources with relative URLs fail to load until you set a real URL. Images also require the `canvas` npm package.
- Synchronous XHR skips your customizations: Dispatchers and interceptors do not apply when page scripts use synchronous `XMLHttpRequest`. The maintainers call this a technical limitation: they "cannot transfer dispatchers or interceptors across a process boundary."
- v30.1.0 shipped regressions that v30.1.2 fixed: The v30.1.2 release fixed severe slowdowns on large DOM trees that were introduced in v30.1.0. It also fixed the rejection of negative CSS sizing values for properties such as `min-width`, which had regressed in the same version.

## Numbers (AgentGid data)
- Gid Score: 83.4 (#51 of all tools, 4 of 122 in its category)
- Used in 615 public projects set up for AI coding agents (9.8%)
- Downloads last week: 166,771,354 (as of 2026-11-24), +42.7% over 4 weeks
- GitHub stars: 21,765 (as of 2026-11-24)
- Last push: 2026-11-21

## Often used with
Vitest 88.3%, Testing Library 67.5%, Vite 68.6%, TypeScript 89.3%, React 68.0%, Tailwind CSS 66.8%

## Checked alternatives (same job)
- Happy DOM: https://agentgid.com/tools/happy-dom/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/jsdom/
- Homepage: https://github.com/jsdom/jsdom#readme
- Repository: https://github.com/jsdom/jsdom
- Source: https://github.com/jsdom/jsdom#readme
- Source: https://github.com/jsdom/jsdom
- Source: https://github.com/jsdom/jsdom/releases


## jsdom: a browser page inside Node.js, for tests

jsdom is a free JavaScript library that builds a web page in memory inside Node.js, without opening a browser. Tests can fill forms, click buttons and read the page, and scripts can pull text and links out of HTML. It does not draw anything, so sizes and positions on screen are not real.

### Where to use it
- Front-end tests without a browser: Your test runner checks that a sign-up form shows "Thanks!" after a valid email, in a fake page that starts in milliseconds.
- Pull data out of saved web pages: A script reads product names and prices from HTML files you downloaded, using the same selectors as in a browser.
- Clean HTML from users on the server: Comments or posts with formatting go through DOMPurify on your server, which needs a page object that jsdom provides.
- Edit a piece of HTML in code: Your build step adds attributes to links or removes empty paragraphs in an HTML snippet before saving it.
- Check generated emails before sending: A test opens each HTML email your app builds and checks that the unsubscribe link and the right name are there. (idea, not from the official pages)
- Test widgets for the page title or focus: Code that changes the page title, focus or stored settings is checked in automated runs without a real window. (idea, not from the official pages)

### How to start
- Add jsdom to your project (it needs Node.js 22.22.2+, 24.15.0+ or 26+).

```
npm install jsdom
```

- Create a page from an HTML string and use window.document like in a browser.

```
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log(dom.window.document.querySelector("p").textContent); // "Hello world"
```


### License
Free to use, change and sell as part of your product under the MIT license. Keep its copyright and license notice in copies of the package you ship.

### Good to know
- It runs in Node.js only, and recent versions need Node.js 22.22.2, 24.15.0, 26 or newer.
- It has no layout. Sizes and positions such as width or offsetTop come back as zero, and clicking a link does not open a new page.
- Scripts inside the HTML do not run unless you turn them on. Turned on for pages from strangers, they can reach your machine.
- It loads no images, styles or frames by default. Pictures and canvas drawing also need the separate @napi-rs/canvas package.
- The project is active; version 31.0.0 came out on November 5, 2026, and changed how canvas support is installed.

Checked 2026-11-11 against: https://github.com/jsdom/jsdom/blob/main/README.md, https://github.com/jsdom/jsdom, https://github.com/jsdom/jsdom/blob/main/LICENSE.txt, https://github.com/jsdom/jsdom/releases/tag/v31.0.0, https://github.com/jsdom/jsdom/releases/tag/v30.0.0, https://registry.npmjs.org/jsdom/latest, https://vitest.dev/guide/environment, https://jestjs.io/docs/configuration, https://github.com/cure53/DOMPurify/blob/main/README.md

### Mistakes to avoid
- Don't: new JSDOM(htmlFromTheInternet, { runScripts: 'dangerously' }) → Do: Keep the default (no scripts) for HTML you did not write; use a real browser in a sandbox if page scripts must run (Page scripts can escape jsdom and run code on your machine.)
- Don't: global.window = dom.window; global.document = dom.window.document; then running tests in Node's global scope → Do: Use the test runner's jsdom environment (Vitest environment 'jsdom', Jest jest-environment-jsdom) or run code inside the jsdom window (The jsdom docs advise against mixing jsdom and Node.js globals.)
- Don't: Asserting on getBoundingClientRect(), offsetTop or visibility in jsdom tests → Do: Test layout and visual results with a real browser (Playwright); keep jsdom tests to content and behavior (jsdom does no layout; in our run getBoundingClientRect().width was 0.)
- Don't: npm install canvas to get canvas and image support with jsdom 31 → Do: npm install @napi-rs/canvas@^1.0.13 next to jsdom (jsdom 31 switched the optional backend; the old canvas package alone no longer enables it.)
- Don't: Reading the document right after new JSDOM(html) when it has scripts that load files → Do: Wait for DOMContentLoaded (or load), checking document.readyState first (Since version 31, parsing pauses at blocking scripts, so the constructor can return a partly built document.)

### Questions
- Will scripts inside my HTML run? Not by default. Pass runScripts 'dangerously' to run them, and only for HTML you trust.
- Can it take screenshots or measure element sizes? No. It does no layout or drawing; use a real browser such as Playwright for that.
- Can it open a live web address? Yes, JSDOM.fromURL() fetches the page and follows redirects; cookies from the response go into its cookie jar.
- Why does my Node.js process not exit after the test? Timers started inside the page keep it running. Call window.close() to stop them.
- Does it work with Vitest and Jest? Yes. Vitest has a built-in jsdom environment, and Jest ships it in the jest-environment-jsdom package.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
