-2

PFB snippet :

let XMLHttpRequest = require('xhr2');
let xhr = new XMLHttpRequest();
xhr.open('GET', 'data.json', true);
xhr.send();

Error : internal/modules/cjs/loader.js:969 throw err; ^

Error: Cannot find module 'xhr2'

I have installed : npm install xmlhttprequest But I still keep getting this error.

Arushi
  • 25
  • 6
  • 4
    How is `xhr2` related to `xmlhttprequest`? Why do you install `xmlhttprequest` when you want to use `xhr2`? Have you tried `npm i xhr2`? – Thomas Sablik Jan 01 '21 at 23:22
  • I did that, but it gives me another error : throw new NetworkError(`Unsupported protocol ${this._url.protocol}`); – Arushi Jan 01 '21 at 23:42
  • https://stackoverflow.com/questions/32604460/xmlhttprequest-module-not-defined-found – Arushi Jan 02 '21 at 00:04
  • You can either install `npm i xmlhttprequest` and use it with `let XMLHttpRequest = require('xmlhttprequest');` or install `npm i xhr2` and use it with `let XMLHttpRequest = require('xhr2');` – Thomas Sablik Jan 02 '21 at 10:34
  • _"I did that, but it gives me another error : `throw new NetworkError(Unsupported protocol ${this._url.protocol});`"_ That's a new, unrelated question. – Thomas Sablik Jan 02 '21 at 10:38

1 Answers1

3

Well, it's telling you that 'xhr2' is not installed, so might as well try npm i xhr2

Srry I would comment but don't have enough reputation or whatever

Unseated
  • 41
  • 1
  • I tried it and still doesn't work. I followed : https://stackoverflow.com/questions/32604460/xmlhttprequest-module-not-defined-found – Arushi Jan 02 '21 at 00:00