Axios is used in the project, then packaged using rollup.
On the browser side, it is normal. But the error is reported on the nodejs side.
Error info:
node test\test.js
error
ReferenceError: XMLHttpRequest is not defined
at dispatchXhrRequest (D:\Project\NodeJs\rollup-axios-error-demo\dist\demo.js:684:21)
at new Promise (<anonymous>)
at xhrAdapter (D:\Project\NodeJs\rollup-axios-error-demo\dist\demo.js:676:12)
at dispatchRequest (D:\Project\NodeJs\rollup-axios-error-demo\dist\demo.js:1082:12)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:266:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
I created a demo that will be available for you to view: rollup-axios-error-demo
The code for axios source code to distinguish the running environment is:
function getDefaultAdapter() {
var adapter;
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
adapter = require('./adapters/xhr');
} else if (typeof process !== 'undefined') {
// For node use HTTP adapter
adapter = require('./adapters/http');
}
return adapter;
}
But after packaging, it becomes:
function getDefaultAdapter() {
var adapter;
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
adapter = xhr;
} else if (typeof process !== 'undefined') {
// For node use HTTP adapter
adapter = xhr;
}
return adapter;
}
Caused the XMLHttpRequest to be instantiated on the nodejs side
In case anyone stumbles upon this, I had the same problem and resolved it by switching import axios from 'axios'
to const axios = require("axios")
in any file where axios was used.