Following this example, I keep getting the error:
TypeError: firebase.storage is not a function
From this line in my code:
google-cloud
. The references and guides for Firebase and Google Cloud do not reflect this as of today.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.
Create Firebase and Google Cloud (Storage) projects.
Using npm, install firebase-admin and google-cloud in Node.js
project.
Note 1: I used the admin SDK, so after creating the Firebase project, you'll need to go to:
Node.js
> [Copy/paste the generated code into
your project] > [click "Generate New Private Key"] > [download the
generated json
to preferred location] > [replace
"path/to...AccountKey.json"
with the path to the key you just
generated]Note 2: the generated key can be reused in firebase or google-cloud credentials.
Once your project is created, import the firebase-admin sdk:
The code should look like this, but filled with your info:
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert("/path/to/generated/json/here.json"),
databaseURL: "database-url-from-firebase"
});
To find the databaseURL, go to 'Storage' in Firebase, and note the URL starting with gs:
and copy/paste it the the value field of databaseURL.
Next, get a reference to the database you can use:
var db = admin.database();
var ref = db.ref("/");
console.log('DB ref: ' + ref); //just to debug, if undefined, there's a problem.
To learn more about reading/writing to the database, follow Firebase's own documentation.
After creating a project on Google Cloud, add billing information; buckets cannot be used without billing info.
In your index.js
:
var gcloud = require('google-cloud');
var gcs = gcloud.storage({
projectId: 'paste-that-project-id-here',
keyFilename: 'paste-that-path-to-the-previously-downloaded-json-from-firebase-here'
});
Now you can send a file to your storage by:
var bucket = gcs.bucket('bucket_name');
var remoteFile = bucket.file('somefile-inThisCaseASong.mp3');
var localFilename = '/Users/you/Music/somefile-inThisCaseASong.mp3';
bucket.upload(localFilename, function(err, file) {
if (!err) {
console.log('somefile-inThisCaseASong.mp3 is now in your bucket.');
} else {
console.log('Error uploading file: ' + err);
}
});
If the file is visible in Firebase Storage and Google Cloud Storage, you're done!
Does it change if you try to do it like the following?
// Initialize the default app:
const app = firebase.initializeApp(appConfig)
// Initialize storage from the *app* object:
const storage = app.storage()
From the docs, it seems that this is an alternate method. I prefer this one, as this links the storage to the app, making the code more readable, and more functional
I encountered a similar problem when I was integrating firebase's storage on the browser (webapp)
<script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-storage.js"></script>
Just add that line, and the bug is gone!
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.
It looks like they have fixed it in a new version of the library.
Just run npm update
on the relevant package(s) and it should work.
I had the same problem, I had my code as following:
import * as firebase from "firebase/app";
import 'firebase/storage';
firebase.initializeApp({
...
});
const storageRef = firebase.storage().ref();
So, I found that way is only if you use Typescript.
If you use only ES6, then you must have:
import firebase from 'firebase/app';
import 'firebase/storage';
firebase.initializeApp({
...
});
const storageRef = firebase.storage().ref();
If you use ES5, then you must have:
var firebase = require("firebase/app");
require("firebase/storage");
firebase.initializeApp({
...
});
const storageRef = firebase.storage().ref();
Moreover, you can also use the following way but it is not recommended because you load all services (database,auth,storage,etc):
import firebase from "firebase";
firebase.initializeApp({
...
});
const storageRef = firebase.storage().ref();
Tested with Firebase 7.15.2