The console informed me yesterday that I should update firebase-functions to v4, we've been on v3.21.2. It also informed me that there would be breaking changes.
All I can find it this: https://github.com/firebase/firebase-functions/releases/tag/v4.0.0, but it'd be great to find a real migration guide? For now I guess I'll search functions wide for the things that are included in that changelog.
Firebaser here. There are more detailed release notes in the docs, but no migration guide, sorry! The most important things to do when migrating to firebase-functions 4.0.0 are:
Use Node 14 or 16 (preferably 16)
Update the firebase-functions, firebase-admin, and firebase-functions-test libraries:
npm install --save firebase-functions#latest firebase-admin#latest firebase-functions-test#latest
There are a couple of changes for specific triggers:
If you're using callable functions & App Check, switch any use of allowInvalidAppCheckToken to enforceAppCheck
If you're using Realtime Database triggers, the DataSnapshot in the event payload will look a little different, but hopefully in a good way - it will now match the DataSnapshot returned by the Admin SDK
Besides that, it's mostly improvements, like better logging, better Typescript types, and new triggers (Remote Config and Test Lab for 2nd-gen functions), as well as a new way to parameterize functions with the params subpackage. It's always recommended to test functions locally with the emulator suite, firebase-functions-test, or functions:shell to catch errors before deploying to production.
Related
Following the Firebase examples to create and deploy a function, I keep failing at the deploy phase.
The error, with --debug enabled, shows:
<<< [apiv2][body] POST
https://cloudfunctions.googleapis.com/v1/projects/actus-poc2/locations/us-central1/functions
{"error":{"code":403,"message":"Cloud Functions uses Artifact Registry
to store function docker images. Artifact Registry API is not enabled
in your project. To enable the API, visit
https://console.developers.google.com/apis/api/artifactregistry.googleapis.com/overview?project=...
Now I was expecting to be able to stay within the confines of the firebase console but this message seems to imply I need to open the Google Cloud Console to enable additional permissions.
Should the code samples better document this?
Or is this a recent change in firebase functions that breaks many of the existing examples?
I need to open the Google Cloud Console to enable additional permissions.
The reason why you need to use the Google Cloud Console is because Cloud Functions for Firebase relies on some Google Cloud services. See.
Function deployments with Firebase CLI 11.2.0 and higher rely on Cloud Build and Artifact Registry.
is this a recent change in firebase functions that breaks many of the existing examples?
Deployments to older versions also do rely on some Google Cloud services. Deployments to older versions use Cloud Build in the same way, but rely on Container Registry and Cloud Storage for storage instead of Artifact Registry.
Should the code samples better document this?
If you do think an update to said documentation could be helpful, here is more about opening Feature requests.
I wonder if Firebase CLI supports updating remote config params and conditions? How do I know if this is something planned to be added in future? As seen here (https://firebase.google.com/docs/cli#config-commands) apparently, we can rollback to different versions but I can't figure out how to update the config template.
Of course Firebase rest API supports that. But I really need it with CLI. Am I missing something?
In the guide for Firestore on deleting a whole collection, there is an example using firebase_tools.firestore.delete. Where can I find documentation about this function? I only found it mentioned in the CLI reference but with little detail.
In most cases the firebase-tools library is only used in the CLI to Firebase projects. So the documentation for that command is in the reference docs for the CLI, but its admittedly a bit lightweight for the Firestore delete command.
Luckily the firebase-tools library is open-source, so you can check exactly what it does by looking at the code. The firestore:delete command is implemented here and here.
If you run into specific problems while implementing similar functionality, or have specific questions about it, I recommend posting those specific problems/questions. For example: the command indeed deletes subcollections.
Using the firebase-cli you can easily deploy functions and show the logs. But I have yet to find the option to download or retrieve cloud functions.
I've dug through quiet a bit of the -h commands and nothing yet.
Is it possible to download / retrieve a projects cloud functions through the firebase-cli? I tend to switch between computers from home to work and I need to go to the console to copy the source code and redeploy.
The Firebase CLI doesn't provide a way to get deployed code. It's expected that you use some form of source control for managing code between developers and machines.
I'm attempting to migrate my Firebase Cloud Functions installation up to the newly released V1.0.
I've carefully followed all instructions provided at
https://firebase.google.com/docs/functions/beta-v1-diff , including including running the updates and also going through every function I have to make sure it reflects the changes in v1 which break old functions.
When I then attempt to run a deploy (for functions only), I get the following error: Error:
Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]". The "credential" property must be an object which implements the Credential interface.
Notably, there is a specific mention of no longer needing to provide the credentials in the argument: "firebase-admin is now initialized without any parameters within the Cloud Functions runtime."
My suspicion is the update to Firebase Cloud Functions (npm install firebase-functions#latest --save) didn't succeed. I suspect this because, although there's a lot of activity after that call, the output is just two lines:
Brandus#1.0.0 /Users/ajr/Documents/dev/sites/Brandus
└── firebase-functions#1.0.0
I've seen another question with the same symptoms: Cloud Functions Firebase v1.0. I tried to comment but my reputation is too low.
Edit: code as requested
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require( 'firebase-functions' ) ;
var fs = require('fs');
var url = require('url');
var http = require('http');
var https = require('https');
// tinycolor2
const tinycolor = require( 'tinycolor2' ) ;
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require( 'firebase-admin' ) ;
//admin.initializeApp(functions.config().firebase);
admin.initializeApp();
That's all the code up to the point where the error is, with the previous code commented out.
I had the same problem. As suggested by Bob Snyder in one of the comments, I checked my versions in package.json and firebase-admin was still to an old version. After setting it to ^5.11.1 and running "npm install" in the functions folder, I was able to deploy right away. I did not have to change the project to TypeScript.
You could just run npm install --save firebase-admin#latest in the functions folder instead. Better this way, no arbitrary version to specify.
I was able to successfully deploy after changing my project to use TypeScript instead of Javascript. The functions appear to be working correctly after the deployment.
I followed these instructions to complete the migration.
It's possible a fresh firebase init would have fixed it even without the migration to typescript, but either way, this worked for me.