A JavaScript implementation of various web standards, for use with Node.js



jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. In general, the goal of the project is to emulate enough of a subset of a web browser to be useful for testing and scraping real-world web applications.

The latest versions of jsdom require Node.js v14 or newer. (Versions of jsdom below v20 still work with previous Node.js versions, but are unsupported.)

Basic usage

  1. ``` js
  2. const jsdom = require("jsdom");
  3. const { JSDOM } = jsdom;
  4. ```

To use jsdom, you will primarily use the JSDOM constructor, which is a named export of the jsdom main module. Pass the constructor a string. You will get back a JSDOM object, which has a number of useful properties, notably window:

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

(Note that jsdom will parse the HTML you pass it just like a browser does, including implied ``, ``, and `` tags.)

The resulting object is an instance of the JSDOM class, which contains a number of useful properties and methods besides window. In general, it can be used to act on the jsdom from the "outside," doing things that are not possible with the normal DOM APIs. For simple cases, where you don't need any of this functionality, we recommend a coding pattern like

  1. ``` js
  2. const { window } = new JSDOM(`...`);
  3. // or even
  4. const { document } = (new JSDOM(`...`)).window;
  5. ```

Full documentation on everything you can do with the JSDOM class is below, in the section "JSDOM Object API".

Customizing jsdom

The JSDOM constructor accepts a second parameter which can be used to customize your jsdom in the following ways.

Simple options

  1. ``` js
  2. const dom = new JSDOM(``, {
  3.   url: "https://example.org/",
  4.   referrer: "https://example.com/",
  5.   contentType: "text/html",
  6.   includeNodeLocations: true,
  7.   storageQuota: 10000000
  8. });
  9. ```

- url sets the value returned by window.location, document.URL, and document.documentURI, and affects things like resolution of relative URLs within the document and the same-origin restrictions and referrer used while fetching subresources. It defaults to "about:blank".
- referrer just affects the value read from document.referrer. It defaults to no referrer (which reflects as the empty string).
- contentType affects the value read from document.contentType, as well as how the document is parsed: as HTML or as XML. Values that are not a HTML mime type or an XML mime type will throw. It defaults to"text/html". If a charset parameter is present, it can affect binary data processing.
- `includeNodeLocations` preserves the location info produced by the HTML parser, allowing you to retrieve it with the `nodeLocation()` method (described below). It also ensures that line numbers reported in exception stack traces for code running inside `