Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
select
✓ import { select } from 'nwsapi';
✗ import select from 'nwsapi';
This is a named export for ESM. For CommonJS, use `const { select } = require('nwsapi');`. Attempting a default import will fail.
configure
✓ import { configure } from 'nwsapi';
✗ import nwsapi from 'nwsapi'; nwsapi.configure();
The `configure` function is a named export used to get a customized NWSAPI engine instance. For CommonJS, use `const { configure } = require('nwsapi');`.
NW.Dom.install
✓ <script type="text/javascript" src="nwsapi.js" onload="NW.Dom.install()"></script>
✗ import { install } from 'nwsapi';
This is a global method exclusively available when NWSAPI is loaded directly in a browser via a `<script>` tag. It automatically polyfills or overrides native DOM methods like `document.querySelector`.
Demonstrates how to install `nwsapi` in a Node.js environment using `jsdom` to simulate a browser DOM, and then how to configure and bind its selector methods (`querySelector`, `querySelectorAll`, `matches`, `closest`) to the `JSDOM` document and `Element` prototype for a familiar API experience.
const { JSDOM } = require('jsdom');
const nwsapi = require('nwsapi');
// Create a JSDOM instance to simulate a browser environment
const dom = new JSDOM(`
<!DOCTYPE html>
<html>
<body>
<div id="app">
<header>
<h1>My Title</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about" class="active">About</a></li>
</ul>
</nav>
</header>
<main>
<section class="content">
<p>Some text here.</p>
<button id="myButton">Click Me</button>
</section>
<section class="footer-content">
<p>More text.</p>
</section>
</main>
</div>
</body>
</html>
`);
// Get the document object from the JSDOM instance
const document = dom.window.document;
// Configure NWSAPI to work with the JSDOM document context
// The library's functions need to be bound to a context that behaves like a DOM element/document.
const engine = nwsapi.configure({
// Example configuration: allow duplicate IDs (default is true)
IDS_DUPES: true
});
// Manually bind NWSAPI methods to the JSDOM document and Element prototype
// This makes it behave like a polyfilled native API.
document.querySelector = (selector) => engine.first(selector, document);
document.querySelectorAll = (selector) => engine.select(selector, document);
dom.window.Element.prototype.matches = function(selector) { return engine.match(selector, this); };
dom.window.Element.prototype.closest = function(selector) { return engine.ancestor(selector, this); };
// --- Using the NWSAPI-enhanced JSDOM document ---
// Find the first element matching a selector
const mainTitle = document.querySelector('h1');
console.log('Main title:', mainTitle ? mainTitle.textContent : 'Not found');
// Find all elements matching a selector
const allParagraphs = document.querySelectorAll('p');
console.log('Number of paragraphs:', allParagraphs.length);
allParagraphs.forEach((p, i) => console.log(`Paragraph ${i + 1}:`, p.textContent));
// Check if an element matches a selector
const aboutLink = document.querySelector('a.active');
if (aboutLink) {
console.log('Is "About" link active?', aboutLink.matches('.active'));
console.log('Is "About" link a button?', aboutLink.matches('button'));
}
// Find the closest ancestor
const myButton = document.getElementById('myButton');
if (myButton) {
const closestSection = myButton.closest('section');
console.log('Closest section to button:', closestSection ? closestSection.className : 'Not found');
}
Errors
Common errors & fixes
TypeError: document.querySelector is not a function
In Node.js or browser environments where NWSAPI is not directly replacing native DOM APIs, the `document.querySelector` and similar methods are not automatically polyfilled or overridden by `nwsapi` upon import.
fixManually bind NWSAPI's functions to the `document` object or `Element.prototype` as demonstrated in the quickstart, or use the NWSAPI functions directly with a context: `engine.first('selector', document.body)`. ReferenceError: NW is not defined
The global `NW.Dom` object is only available when NWSAPI is loaded directly into a browser environment via a `<script>` tag. This error occurs when trying to access `NW.Dom` in Node.js or before the script has executed in a browser.
fixFor Node.js, use `require('nwsapi')` or `import { ... } from 'nwsapi'` to access the module exports. For browsers, ensure `nwsapi.js` is loaded via a `<script>` tag before attempting to access `NW.Dom`. Audit
Dependencies
No dependency data recorded yet.