dom-iterator is a JavaScript library providing a feature-rich, bidirectional iterator for traversing DOM nodes. It offers an enhanced alternative to the native `NodeIterator` API, capable of moving both forwards and backward through the DOM tree. The library allows for advanced filtering of nodes using `select` and `reject` methods based on `nodeType`, string expressions, or custom functions, and includes functionalities like `peek` for non-destructive inspection and methods to jump to `opening()` or `closing()` tags. Currently at version 1.0.2, the package appears to be unmaintained; it was last published on npm a year ago (as of April 2026) and its GitHub repository shows the last commit also from a year ago. Its primary usage pattern is CommonJS, reflecting its age, and it requires a separate DOM implementation (like `mini-html-parser`) to function in Node.js environments.
npm install dom-iteratorVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates initializing `dom-iterator` with `jsdom` for a Node.js environment, then using `next()` to traverse all nodes, filtering with `next(Node.TEXT_NODE)`, and traversing backward with `prev(Node.ELEMENT_NODE)`. It also showcases the `select()` method for compound filtering.
Use `const iterator = require('dom-iterator');` in CommonJS files. For ESM projects, either configure your bundler for CJS interoperability or wrap the `require` call in a shim.Evaluate alternatives for active maintenance. If retained, thorough testing for compatibility and potential issues is required, especially in modern environments.
Install and initialize a DOM parsing library (e.g., `npm install jsdom`). Pass elements from this library to `dom-iterator` and extract `Node` constants from its `window` object.
Always install via npm (`npm install dom-iterator`) and ignore references to `component(1)`.
In Node.js, instantiate `jsdom` or similar library and extract the `Node` object from its `window` property. Example: `const { JSDOM } = require('jsdom'); const { window } = new JSDOM(''); const { Node } = window;`If in a CJS file, use `const iterator = require('dom-iterator');`. If in an ESM file, and a bundler is not used or configured for CJS interop, you might need to use `import * as domIterator from 'dom-iterator'; const iterator = domIterator;` or consider using a wrapper or direct `require` if supported by your runtime.Ensure the package is installed by running `npm install dom-iterator` in your project's root directory. Verify that `node_modules/dom-iterator` exists.