Following this example, I keep getting the error:
TypeError: firebase.storage is not a function
From this line in my code:
var storageRef = firebase.storage().ref();
(And when I simply try to initialize storage from the storage guide, linked from firebase's npm site, I get the same error.)
In my Node.js project, I'm including the following libraries:
const firebase = require('firebase');var admin = require('firebase-admin');const fs = require('fs');
Up to this point, I've successfully been able to read from and write to the firebase database, creating a reference to the database with var db = admin.database(), then var ref = db.ref("/")... So I know I've configured Firebase and firebase-database correctly. But I'm stuck on storage, and have tried both admin.storage().ref() and firebase.storage().ref(), and firebase.storage().ref("/") with the same error message.
I've also tried:
var storage = firbase.storage();
var storageRef = storage.ref();
and
const app = firebase.initializeApp(config);
var storage = app.storage();
and with ref()'s void argument () and with "/"... but have the same message, yet to no avail.
I'm using:
- "firebase": "^3.6.4"
- "firebase-admin": "^4.0.4"
- Node.js : v6.9.1
What must I do to successfully create a reference to storage?
17 Answers
I faced the same problem. In my case, I needed to include storage module besides Firebase core.
import firebase from 'firebase/app';
import 'firebase/storage'; // <----
firebase.initializeApp({
...
});
const storageRef = firebase.storage().ref();
(npm firebase v5.0.4)
DEPRECATED, see below:
According to this answer, instead of firebase storage, in Node.js, google-cloud package storage should be used, and it seems that this answer should confirm it. Code example:
npm i --save google-cloud
Then:
const gcloud = require('google-cloud')
const storage = gcloud.storage({
projectId: '<projectID>',
keyFilename: 'service-account-credentials.json',
});
const bucket = storage.bucket('<projectID>.appspot.com')
As of 2018, this is the correct answer:
Or using only the storage part of the package:
npm install --save @google-cloud/storage
And then:
var storage = require('@google-cloud/storage')
Also check the docs for more.
Deprecated: please see the accepted answer.
Some details to note:
- Firebase Storage is no longer used with Node.js, so all documentation there is useless for Node.js. Instead, use
google-cloud. The references and guides for Firebase and Google Cloud do not reflect this as of today. - Unlike Firebase, google-cloud costs money, even for small projects.
- In my case, I'm using the
firebase-adminSDK so I don't have to mess with user authentication at the moment.
Purpose
To create a single Node.js project which uses Firebase and Google Cloud. Why? Firebase has a useful database, among other features, and Google Cloud allows cloud file storage and retrieval.
Directions
Step 1: Project Creation
Create Firebase and Google Cloud (Storage) projects.