将代码与webpack4捆绑在一起后,firebase软件包无法识别我的节点环境并抛出错误。
我的代码很简单:
const firebase = require("firebase").default;
const config = {
...
};
const firebaseUser = {
"email": "abc@gmail.com",
"password": "pass1234"
}
var app = firebase.initializeApp(config);
firebase.auth().signInWithEmailAndPassword(firebaseUser.email, firebaseUser.password)
.then(({ user }) => {
console.log(user.uid);
});
如果我运行以下命令,则一切正常:
node ./index.js
但是,如果我尝试将其与以下webpack 4配置捆绑在一起。
webpack --mode development --verbose --progress
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
target: 'node'
};
我收到以下错误:
Error: The XMLHttpRequest compatibility library was not found.
因此我搜索了Firebase源代码,可以看到错误是从此行开始的-https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/rpchandler.js#L134
// Get XMLHttpRequest reference.
var XMLHttpRequest = fireauth.RpcHandler.getXMLHttpRequest();
if (!XMLHttpRequest && !fireauth.util.isWorker()) {
// In a Node.js environment, xmlhttprequest module needs to be required.
throw new fireauth.AuthError(fireauth.authenum.Error.INTERNAL_ERROR,
'The XMLHttpRequest compatibility library was not found.');
}
...
/**
* @return {?function(new:XMLHttpRequest)|undefined} The current environment
* XMLHttpRequest. This is undefined for worker environment.
*/
fireauth.RpcHandler.getXMLHttpRequest = function() {
// In Node.js XMLHttpRequest is polyfilled.
var isNode = fireauth.util.getEnvironment() == fireauth.util.Env.NODE;
var XMLHttpRequest = goog.global['XMLHttpRequest'] ||
(isNode &&
firebase.INTERNAL['node'] &&
firebase.INTERNAL['node']['XMLHttpRequest']);
return XMLHttpRequest;
};
我不明白为什么fireauth.RpcHandler.getXMLHttpRequest函数没有在bundle.js中返回XMLHttpRequest实例。
我的webpack配置有问题吗?