Firebase-js-sdk: XMLHttpRequest is not defined -> when using storage -module on nodejs

Created on 5 Dec 2017  路  9Comments  路  Source: firebase/firebase-js-sdk

Environment

  • Operating System version: Linux
  • Firebase SDK version: 4.7.0
  • nodejs version: 8.9.1
  • Firebase Product: firebase-js-sdk storage

Problem and steps

When using storage module on nodejs side I'll get an error.

this.xhr_ = new XMLHttpRequest();
                        ^

ReferenceError: XMLHttpRequest is not defined

Seems that is missing nodejs support.

Example code

var storageRef = firebase.storage().ref();
var ref = storageRef.child('mountains.jpg');
var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
ref.put(bytes).then(function(snapshot) {
  console.log('Uploaded an array!');
});

Most helpful comment

There's a workaround, that involves adding XMLHttpRequest to node.

global.XMLHttpRequest = require("xhr2");

I don't know if this solves every issue, but it works for what I need.

All 9 comments

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information.

This is working as intended, the Firebase Storage SDK doesn't support Node.js environments right now (see: https://github.com/firebase/firebase-js-sdk/issues/18)

Feel free to +1 that issue (with the Github reaction!)

@jshcrowthe I am getting the same error trying to call firebase.auth().signInWithEmailAndPassword()

{ [Error: The XMLHttpRequest compatibility library was not found.]
  code: 'auth/internal-error',
  message: 'The XMLHttpRequest compatibility library was not found.' }

node version 8.9.3

I figured out why

So you can't do this:

var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');

var app = firebase.initializeApp({ ... });

You have to import entire 'firebase' or you will get the error.

May want to fix Readme about this.

@joonhocho That is a different issue that is probably worth taking a look at. I'll make an issue and see if we can get that addressed.

There's a workaround, that involves adding XMLHttpRequest to node.

global.XMLHttpRequest = require("xhr2");

I don't know if this solves every issue, but it works for what I need.

+1

For Using TypeScript, wrap global as any

// Polyfills required for Firebase
(global as any).XMLHttpRequest = require('xhr2');
(global as any).WebSocket = require('ws'); // May also come in handy
Was this page helpful?
0 / 5 - 0 ratings