question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Axios in a node worker_thread get "XMLHttpRequest is not defined" error

See original GitHub issue

Describe the bug

When you use axios in a node’s worker thread the library returns this error message: “XMLHttpRequest is not defined”

To Reproduce

//Main thread:
import { Worker } from 'worker_threads'
const worker = new Worker("./worker.js");

//Listen for a message from worker
worker.postMessage("run!");

// worker.js file:
import { parentPort } from 'worker_threads'
import Axios from 'axios';

const getRequest = async function() {
    const tiresias = await Axios({
      baseURL: 'https://example.com/hades/v2',
      url: '/autocomplete/predict',
      method: 'get',
      params: { s: 'tets', min: 0.1 },
    });
}
parentPort.once('message', message => getRequest());  

Expected behavior

I don’t understand why this error occurs and how to fix it. (“XMLHttpRequest is not defined”)

Environment

  • Axios Version: 0.21.1
  • Adapter: Good question, I guess HTTP (node env), but I don’t know why the error says “XMLHttpRequest…”
  • Browser: not necessary, a server side problem
  • Browser Version: not necessary, a server side problem
  • Node.js: 14.17.0
  • OS: Bis Sur 11.5.2
  • Additional Library Versions: Next.js framework

Additional context/Screenshots

Schermata 2021-10-30 alle 10 05 12

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
ale-grossellecommented, Oct 30, 2021

To workaround the problem I do this:

// worker.js file:
import { parentPort } from 'worker_threads'
import Axios from 'axios';

//**********FORCE TO USE http adapter (node.js version):*****
 Axios.defaults.adapter = require('axios/lib/adapters/http.js');
//**********FORCE TO USE http adapter (node.js version):*****
const getRequest = async function() {
    const tiresias = await Axios({
      baseURL: 'https://example.com/hades/v2',
      url: '/autocomplete/predict',
      method: 'get',
      params: { s: 'tets', min: 0.1 },
    });
}
parentPort.once('message', message => getRequest());  

This solve the problem and all work as exptected.

0reactions
pleek91commented, Oct 14, 2022

I have not, very frustrating

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Axios in a node worker_thread get "XMLHttpRequest is ...
Describe the bug When you use axios in a node's worker thread the library returns this error message: "XMLHttpRequest is not defined" To ......
Read more >
Axios with node.js - ReferenceError: XMLHttpRequest is not ...
And been getting the following error: ReferenceError: XMLHttpRequest is not defined at dispatchXhrRequest (C:\...\node_modules\axios\dist\axios ...
Read more >
XMLHttpRequest is not defined Error in JavaScript | bobbyhadz
The "XMLHttpRequest is not defined" error occurs for 2 main reasons: Trying to use the XMLHttpRequest in a Node.js application (on the server...
Read more >
Node.js - ReferenceError: XMLHttpRequest is not defined
Attempting to run the following JavaScript code (an AJAX call using XMLHttpRequest) throws a ReferenceError under Node, but works in a web browser....
Read more >
get data with axios stack overflow Code Example - Grepper
Answers related to “get data with axios stack overflow” · limit data with axios in react js · send xmlhttprequest with axios ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found