I am getting an error : XMLHttpRequest is not defined by running the bundled code in nodejs.
in the source code of axios it is:
function getDefaultAdapter() {
var adapter;
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
adapter = require('./adapters/xhr');
} else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
// For node use HTTP adapter
adapter = require('./adapters/http');
}
return adapter;
}
but after bundled by webpack@5, all the conditions get into the same module 13, This is where the error occurs:
function getDefaultAdapter() {
var adapter;
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
adapter = __webpack_require__(13);
} else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
// For node use HTTP adapter
adapter = __webpack_require__(13);
}
return adapter;
}
But I really don't know why and how to solve this problem. this is the mini repo: code-sample
just set webpack.config.js
{
// ...
target: ['web', 'node'],
// ...
}
I thought webpack5 does not support the target attribute, which was obviously my mistake