Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mikamaunula opened this issue Dec 5, 2017 · 9 comments
Closed

Comments

@mikamaunula
Copy link

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!');
});
@google-oss-bot
Copy link
Contributor

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
Copy link
Contributor

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

@jshcrowthe
Copy link
Contributor

jshcrowthe commented Dec 5, 2017

This is working as intended, the Firebase Storage SDK doesn't support Node.js environments right now (see: #18)

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

@joonhocho
Copy link

joonhocho commented Feb 16, 2018

@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
Copy link

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.

@jshcrowthe
Copy link
Contributor

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

@pmoleri
Copy link

pmoleri commented Apr 6, 2018

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.

@indhio
Copy link

indhio commented Apr 23, 2018

+1

@Methodician
Copy link

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

@firebase firebase locked and limited conversation to collaborators Oct 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants