Using Axios in a node worker_thread get "XMLHttpRequest is not defined" error
See original GitHub issueDescribe 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To workaround the problem I do this:
This solve the problem and all work as exptected.
I have not, very frustrating