jsdom
is a JavaScript implementation of the DOM designed for Node.js, allowing you to parse and manipulate HTML as if you were in a browser environment. It is commonly used for web scraping, testing, and server-side DOM manipulation.
npm install jsdom
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const response = await fetch(url);
const html = await response.text();
const dom = new JSDOM(html);
const document = dom.window.document;
Once the document is available, you can use standard DOM methods to extract elements.
const title = document.querySelector("title").textContent;
console.log("Page Title:", title);
const links = [...document.querySelectorAll("a")].map(a => a.href);
console.log("All Links:", links);
You can manipulate the document just like in a browser.
document.body.innerHTML = "<h1>Hello, jsdom!</h1>";
console.log(document.body.innerHTML);