Skip to content
Advertisement

How do I use firebase storage from a client side React app?

I’m trying to write a file into cloud storage from within my React app. I created a new React app using the create-react-app tool. I believe I have followed the correct instructions to get a storage reference, with code like this: this.storage = firebase.storage();. But, no matter how I configure firebase and no matter what I import, I see this error: TypeError: _this.firebase.storage() is not a function..

I tried importing @firebase/storage (commented out here) but this results in an error, and the NPM page for that module suggests this is not something a user should ever load directly.

I believe I added the proper firebase libraries. I can use firestore and the legacy database (I removed the code to do it, but it works if I use something like this.firestore = this.firebase.firestore();). But, storage is unresolved.

I’m a little unclear as to whether I should be using client libraries for cloud storage (I believe that is exactly the same thing as “storage” for Firebase). But, all the examples seem to be for accessing storage from a NodeJS app, and I want to write the file from within my browser context as client-side JavaScript.

My package.json looks like this:

JavaScript

My index.js looks like this:

JavaScript

The firebase config is copied from the Firebase console (but changed here).

Advertisement

Answer

This works: $ npm i @firebase/storage --save.

This currently results in "@firebase/storage": "^0.2.3" inside package.json.

If I have that, I can use code like this in my firebaseConfig.js file to setup storage inside firebase:

JavaScript

Then code like this works:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement