Registry / http-networking / w3c-xmlhttprequest

w3c-xmlhttprequest

JSON →
library3.0.4jsnpmunverified

w3c-xmlhttprequest is a Node.js library that provides a server-side implementation of the XMLHttpRequest (XHR) API, faithfully adhering to the W3C/WHATWG Living Standard. It allows Node.js applications to make HTTP requests using an API familiar to front-end developers, bridging the gap between browser and server environments for XHR-based logic. The current stable version is 3.0.4. The project appears to have an active maintenance cadence, with recent patch releases addressing bug fixes and type improvements. Its key differentiators include its strict adherence to the W3C specification, providing predictable behavior consistent with browser-based XHR, and its utility for isomorphic JavaScript codebases or server-side rendering where XHR patterns are preferred over Node.js native `http` or `https` modules.

npm install w3c-xmlhttprequest
INSTALL
IMPORT
SIG · W3C-XMLHTTPREQUEST
W
w3c-xmlhttprequest
http-networkingjavascriptv3.0.4
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

XMLHttpRequest
✓ import { XMLHttpRequest } from 'w3c-xmlhttprequest';
✗ const XMLHttpRequest = require('w3c-xmlhttprequest');
For CommonJS, use destructuring: `const { XMLHttpRequest } = require('w3c-xmlhttprequest');`. Versions 3.0.0-3.0.3 had a regression with `require` support, fixed in v3.0.4.
XMLHttpRequest (Type)
✓ import type { XMLHttpRequest } from 'w3c-xmlhttprequest';
TypeScript types are included and available for explicit import.
NodeXMLHttpRequest
✓ import { XMLHttpRequest as NodeXMLHttpRequest } from 'w3c-xmlhttprequest';
Often aliased to avoid conflict with global browser XMLHttpRequest types in mixed environments.

Demonstrates a basic asynchronous GET request to 'https://example.com/', logging the HTTP status and response details upon completion, including error handling.

import { XMLHttpRequest } from 'w3c-xmlhttprequest'; // This example demonstrates a basic GET request using the server-side XMLHttpRequest. // It fetches content from a public URL and logs a confirmation message upon successful load. // Note that network requests in Node.js are asynchronous and non-blocking. const client = new XMLHttpRequest(); client.open('GET', 'https://example.com/'); // Specify the HTTP method and the target URL. client.addEventListener('load', () => { // The 'load' event fires when the transaction is complete, regardless of success or failure. // Check client.status for HTTP status codes (e.g., 200 for success). console.log('Received an HTTP response. Status:', client.status); if (client.status >= 200 && client.status < 300) { console.log('Response data length:', client.responseText.length, 'characters.'); } else { console.error('Request failed with status:', client.status); } }); client.addEventListener('error', (event) => { console.error('Network error occurred:', event); }); client.send(); // Initiates the request. console.log('GET request sent to example.com...'); // This will log immediately.
Debug
Known issues
breakingVersion 3.0.0 introduced significant changes to align strictly with the W3C/WHATWG Living Standard for XMLHttpRequest. This included enhanced support for features like `EventListenerObject`, `xhr.responseURL`, and `xhr.withCredentials`. Code relying on pre-v3.0.0 behavior or less strict XHR implementations may require updates.
fix
Review your XHR usage against the W3C specification, especially event listener registration and property access. Update code to conform to standard XHR behavior.
affects: >=3.0.0
gotchaVersions 3.0.0 through 3.0.3 of `w3c-xmlhttprequest` experienced a regression where `require()`-style CommonJS imports might not have functioned correctly or consistently, leading to errors when attempting to instantiate `XMLHttpRequest`.
fix
Upgrade to `w3c-xmlhttprequest@3.0.4` or later for full CommonJS compatibility. Alternatively, use ES module `import` syntax if your environment supports it.
affects: >=3.0.0 <3.0.4
gotchaThe package requires Node.js version 10.13.0 or higher. Using it with older Node.js versions will result in installation or runtime failures.
fix
Ensure your Node.js environment meets the minimum requirement of 10.13.0 or upgrade Node.js.
affects: <10.13.0 Node.js
gotchaWhile `w3c-xmlhttprequest` mimics browser XHR, it operates in a server-side Node.js environment. This means certain browser-specific behaviors, like automatic cookie handling, credential management, or CORS preflight logic (beyond the base HTTP request), are not automatically replicated and might require manual implementation or additional Node.js modules.
fix
Be mindful of environmental differences. For features like cookie management or complex credential handling, integrate appropriate Node.js HTTP utilities or proxy setups if needed.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: XMLHttpRequest is not a constructor
Attempting to import `XMLHttpRequest` incorrectly using CommonJS `require()` in versions 3.0.0-3.0.3, or an incorrect import path.
fix
Ensure your package version is `w3c-xmlhttprequest@3.0.4` or higher for reliable CommonJS. Use destructuring for `require`: `const { XMLHttpRequest } = require('w3c-xmlhttprequest');`. For ESM, use `import { XMLHttpRequest } from 'w3c-xmlhttprequest';`.
TypeError: EventTarget.prototype.addEventListener called with a non-object argument
Passing an invalid or non-object argument to the `addEventListener` method, which expects an `EventListener` or `EventListenerObject`.
fix
Verify that the argument passed to `addEventListener` is a valid function or an object implementing `handleEvent()`, as per the W3C EventTarget specification. This issue was addressed in beta versions leading to 3.0.0.
Upgrade
Version history
3.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
2
Anthropic
1
Resources