firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

XMLHttpRequest is not defined -> when using storage -module on nodejs

Open mikamaunula opened this issue 8 years ago • 9 comments

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!');
});

mikamaunula avatar Dec 05 '17 07:12 mikamaunula

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

google-oss-bot avatar Dec 05 '17 07:12 google-oss-bot

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

google-oss-bot avatar Dec 05 '17 07:12 google-oss-bot

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 avatar Dec 05 '17 17:12 jshcrowthe

@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

joonhocho avatar Feb 16 '18 01:02 joonhocho

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 avatar Feb 16 '18 01:02 joonhocho

@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.

jshcrowthe avatar Feb 20 '18 18:02 jshcrowthe

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.

pmoleri avatar Apr 06 '18 15:04 pmoleri

+1

indhio avatar Apr 23 '18 18:04 indhio

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

Methodician avatar Jul 17 '19 18:07 Methodician