Skills2026年5月13日·1 分钟阅读

jsdom — JavaScript DOM Implementation for Node.js

A pure-JavaScript implementation of web standards that lets you run browser-like DOM operations in Node.js without a real browser.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
jsdom Overview
直接安装命令
npx -y tokrepo@latest install e7160e2f-4ee6-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards. It allows you to create and manipulate a browser-like DOM environment entirely in Node.js, which is essential for server-side rendering, testing, and scraping.

What jsdom Does

  • Parses HTML and produces a full DOM tree accessible via standard APIs
  • Implements core web standards including DOM, HTML, URL, and Fetch
  • Executes inline and external JavaScript within the simulated document
  • Supports custom user agents, referrers, cookies, and content types
  • Provides WHATWG URL and Encoding APIs for standards-compliant parsing

Architecture Overview

jsdom uses parse5 as its HTML parser to construct a DOM tree that conforms to the WHATWG DOM specification. The tree is wrapped in a Window object that exposes standard APIs like document, navigator, and console. Script execution runs through the Node.js VM module with configurable resource loading. Each JSDOM instance is isolated with its own window scope, making parallel test runs safe.

Self-Hosting & Configuration

  • Install via npm or yarn into any Node.js project (no native deps required)
  • Set url, referrer, and contentType options when creating a new JSDOM instance
  • Configure runScripts: "dangerously" to execute scripts inside the DOM
  • Use pretendToBeVisual: true to enable requestAnimationFrame and cancel timers
  • Control external resource loading with the resources option and custom loaders

Key Features

  • Full WHATWG DOM Level 4 implementation in pure JavaScript
  • No headless browser binary needed, keeping CI environments lightweight
  • Supports cookies, session history, and navigation within the virtual DOM
  • Works with popular testing frameworks like Jest, Mocha, and Vitest out of the box
  • Processes CSS selectors via querySelector and querySelectorAll for DOM traversal

Comparison with Similar Tools

  • Cheerio — faster for static HTML scraping but does not execute JavaScript or emulate a live DOM
  • happy-dom — lighter alternative focused on speed, but implements fewer web standards
  • Puppeteer — controls a real Chromium browser for full rendering fidelity, at the cost of heavier resource usage
  • linkedom — memory-efficient DOM implementation but lacks script execution support

FAQ

Q: Does jsdom support modern CSS features? A: jsdom does not implement a full CSS engine or layout. It parses stylesheets but does not compute styles or render visually.

Q: Can I use jsdom with Jest? A: Yes. Set testEnvironment: "jsdom" in your Jest config and Jest will use jsdom as the default DOM environment for all tests.

Q: How does jsdom handle fetch and XMLHttpRequest? A: jsdom provides built-in implementations of both when resource loading is enabled, routing requests through Node.js HTTP modules.

Q: Is jsdom suitable for web scraping? A: It works well for scraping pages that require JavaScript execution. For static HTML parsing, Cheerio is typically faster.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产