Firebase: deploy a second function - firebase

I need to deploy a second cloud function. I've noticed than when I deploy it with firebase deploy --only functions:secondFunction, the process skips the 'functions: updating Node.js 16 function app(us-central1)...' step and says deploy complete yet the cloud function remains unchanged. Kindly help

This is a known issue in this github thread1 & thread2, it is a result of the nature of Typescript's ability to import things outside of the obvious source tree.
typescript is building into lib/functions/index.js instead of
lib/index.js, thus package.json is pointing to the wrong file, and
deploying an older compiled .js file.
So you can either
edit "main" in the package.json "main":"lib/index.js" to "main":"lib/functions/src/index.js" to point at the new location
remove imports and rebuild, then it went back to the building it at lib/index.js
You can also check this stackoverflow example to properly export the function and deploy in javascript

Related

Firebase Hosting with Environment Variables?

My Firebase application has dev & prod environments (two separate Firebase projects).
Issue: The config passed to firebase.initializeApp() is environment-dependent. Currently, I have this hardcoded. If I want to deploy to dev, I comment-out the initializeApp call with the prod config, and visa versa. Is there anyway to do this intelligently with environment variables? For instance, when I run the commands:
firebase use prod
firebase deploy --hosting
can 'prod' be passed as an environment variable to the javascript code so it knows to pass the prod-config to firebase.initializeApp()?
This is a fairly common development scenario and Angular has you covered. Beyond Firebase there are probably other options you may want to change depending on whether you are testing/deploying in development or production.
Check out the Angular docs for Building and serving Angular apps. This has all the info detailing the usage of this.
They make use of an environment.ts file, which most likely would include your Firebase development config, and an environment.prod.ts file, which would include your Firebase production config.
In your angular.json file you detail the configuration options where if you are building with a production option it would replace the environment.ts file with your envionrment.prod.ts file.
When using ng build it would use your dev config. When ready to test and deploy your app you would add a configuration switch, such as ng build --configuration=production

Cloud Functions deploy produces annoying "path is deprecated" warning with no indication from firebase about a replacement

Firebase cloud functions database.d.ts has the following comment which throws warnings in my IDE and when I deploy my project:
/** #deprecated Removed in next major release to match Web SDK typings. */
path: string;
In many of my cloud functions, I create dictionaries with the keys being paths to realtime database nodes whenever I want to update entries.
This is the exact behavior I need and works without any issues. However, every time I deploy my cloud functions I'm greeted with over 50 warnings that:
path is deprecated: Removed in next major release to match Web SDK typings.
The terminal call that creates these warnings is:
Running command: npm --prefix "$RESOURCE_DIR" run lint
Without the path variable, I'd have to create a custom function to create a path from the DatabaseReference's key and parent keys. That sounds absurd. Is this possibly a lint issue?
There's nothing to indicate in the documentation that they're removing the path variable, and I hate seeing this warning spammed every time I deploy my functions. Is this a legitimate warning, and if so, is there a replacement way of getting the path from a DatabaseReference built into the sdk?
path property deprecated in version 9+ of the node SDK –
Try do a ref.toString() and remove the base URL
Consider:
var adaRef = firebase.database().ref("users/ada");
adaRef.toString()
Will print the full URL: https://firebaseio.com/users/ada
So to just get the path, you substring it out of there. Two ways of doing that are:
adaRef.toString().substring(firebase.database().ref().toString().length-1)
Or:
adaRef.toString().substring(adaRef.root.toString().length-1)
both will print /users/ada
Looking at this pull request[1] on the Admin SDK open-source repo, it looks like the path property was deprecated to create type compatibility between the JavaScript/web SDK and the Node.js Admin SDK.
The client-side JavaScript SDK for Firebase Realtime Database never had a path property. The way to get the path has always been to call toString() explicitly, or to include the reference in a string concatenation.

Unable to change the default project to deploy my project on firebase hosting

i am trying to deploy my project on firebase hosting. And whenever i am using firebase init it is showing error in terminal as "firebaserc already has a default project " and it exit with that error
i have tried firebase logout and firebase login again . And used "firebase use" command also to change the project but it is still performing the firebase init action on the default project
i want to remove that default project
If you look very carefully at the messaging, it's saying that the name of the file is ".firebaserc" with a leading dot. This file indicates that firebase init was already run in this folder, and the contents of that file describe which project it's connected to (flairboat-48f7b). If you no longer want that file, delete it and start over. Since it starts with a dot, it might be hidden from normal view, but you can be sure that it exists.
you should write:
firebase use --add
and it work! You get the option to choose the preject from firebase.
I ran into the same issue.
Due to a small mistake in setting up the project in firebase, I had to delete it.
But later, I realized that my application NEEDS the default project to be hosted from firebase.
Since I found no shortcuts and running out of time, I did "this" to fix the issue in 10 minutes...with a 5-Step-Process. This is not a "Clever tip". But if you want things up and running soon, you can try this...
Create a new project (in my case, it's React project in VS Code IDE) using - npx create-react-app newprojectname to create a new react project with a different file name.(Don't delete or replace your previous project yet... )
While the new project is being created, create a new project in firebase to host your project.
Copy the folder from your old project that has all your work (it's "src" folder in case of React) and replace the "src" folder in the new project you created in your local machine.
Install all dependencies...Don't forget to add any dependencies you added to you old project. Look at the package.json file of the old project and import all dependencies.
Hit the start command (npm start in my case) and see your project running.
*I'll update if I found some firebase secret to resolve this issue. You can look for the same.
Ensure your firebase project support email and firebase initialize login email are same. If different? It won't be worked. so you must ensure it that two email (firebase project support email and firebase initialize email) are same.

How to properly do tree shaking to reduce bundle size and separate entry point for each cloud function

I am using Google Firebase Cloud Functions with TypeScript, and I found out that even though each function is deployed separately, they all share the same bundles and dependencies, even if some functions do not use them nor import them.
In my case, one cloud function uses Redis and others don't. I have 10 functions. All 10 functions actually end up importing redis related code even though they don't import them.
Since all functions share the same entry point, index.js. It currently seems it's impossible to have separate tree-shaken bundles / entry points for each function.
This is very inefficient in terms of bundle size / cold start timing / memory / etc. It also means as I have more and more functions, bundle size will grow for all functions together. It's not scalable.
Is there any way to not share the entry point, index.js, and have completely separate bundles by using bundlers like webpack?
You can create a different local Firebase working area (with firebase init) for each function that should deploy in isolation from the others. You will have to instruct the CLI not to overwrite the other functions on deployment using the --only functions:yourFunctionName to deploy it.
Or, you can deploy function using Cloud tools (gcloud) instead of Firebase tools, but you won't be able to use firebase-functions and its TypeScript bindings.
Or, you can lazily load your modules instead of statically loading them at the global scope of your functions, as described in this video.
I don't recommend using webpack. It's not going to be worth your time to get it configured.
You might try better-firebase-functions, which solves this elegantly by automatically lazy loading only the function that is currently invoked by checking the environment variable FUNCTION_NAME - see https://link.medium.com/4g3CJOLXidb

Can't deploy a function in Firebase which include #google-cloud/storage

I'm trying to deploy a function in firebase cloud functions to be triggered when a new file is uploaded in the storage.
I've included the google-cloud/storage lib to manage some download but it seems there's an error in the lib preventing me to deploy :
node_modules/#google-cloud/common/build/src/service-object.d.ts(72,45): error TS8020: JSDoc types can only be used inside documentation comments.
node_modules/#google-cloud/common/build/src/service-object.d.ts(72,45): error TS8028: JSDoc '...' may only appear in the last parameter of a signature.
node_modules/#google-cloud/common/node_modules/google-auth-library/build/src/auth/oauth2client.d.ts(291,55): error TS1039: Initializers are not allowed in ambient contexts.
Does anyone face the same issue ? any workaround ? downgrade lib to older version or newer for TypeScript ?
Thanks,
Nanex

Resources