Firebase + Node.js + Webpack 4:错误:找不到XMLHttpRequest兼容性库

时间:2018-07-21 09:22:51

标签: javascript node.js firebase webpack

  • Nodejs 8.11.1
  • Firebase 5.3.0
  • Webpack 4.8.1

将代码与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配置有问题吗?

0 个答案:

没有答案