XMLHttpRequest is a built-in object in web browsers.
It is not distributed with Node. The http module is the built-in tool for making HTTP requests from Node.
Most people making HTTP requests from node use a third party library with a friendlier API. Two popular choices are Axios (for use both in Node.js and browsers) and node-fetch
(which implements the fetch API which is built into browsers and is a modern replacement for XMLhttpRequest.
2022 update: Node 18 has a native implementation of fetch
enabled by default.
If you really want to use XHR in Node.js then there are a couple of third party implementations. xmlhttprequest
(which seems to be unmaintained) and xhr2
(which has had an update this year).
-
Install it with npm,
npm install xhr2
-
Now you can
require
it in your code.var XMLHttpRequest = require('xhr2'); var xhr = new XMLHttpRequest();