When initializing a firebase project, it creates a firebase.json file containing settings for hosting, firestore, storage, but nothing for functions. When deploying, the firebase CLI correctly finds the functions. Does the CLI goes through the whole project looking for functions or does it store the functions' folder path somewhere?
The Firebase CLI by default assumes that the Cloud Functions for your projects are in a functions folder under the project folder.
You can override this by specifying a source value in the functions configuration in firebase.json.
Also see Doug's answer here: How to deploy functions from other directory than '/functions'?
Related
I'm trying to deploy my firebase project, but Im getting the following error:
=== Deploying to 'my-proj'...
deploying firestore, functions, hosting
cloud.firestore: checking firestore.rules for compilation errors...
[W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]).
cloud.firestore: rules file firestore.rules compiled successfully
Error: Could not detect language for functions at
any thoughts?
In my case, I missed functions init:
firebase init functions
documentation
In my case I use firebase deploy --only hosting command
I can't tell what else is perhaps going wrong with your code, but I also had an error that looked like this part:
[W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]).
Some of your firebase rules are written using version 1. You may need to go in and add
rules_version = '2';
to the top of your firebase rule set, specifically for Firebase Firestore. You can do this in the firebase console, or in the firestore.rules in your project - which may be a newly generated file in your project.
For anyone else who ends up here. I had setup firebase static hosting 1yr+ ago, and at that point in time hosting was a "function". Since then, hosting is now a distinct offering separate of functions.
You need to convert the previous "default" function (which 'was' hosting) to hosting offering.
Delete (or edit) firebase.json in the root directory of your firebase project.
Firebase init hosting -> This will create a new "public" folder.
Copy your previous functions/public folder to overwrite the new one.
Delete the previous functions folder altogether.
Now hosting is fixed and you can deploy again.
Firebase init functions -> This will create a new functions folder, and all of the boilerplate json and .js files.
The problem I was having is the functions folder had both hosting and functions stepping on each other within the functions directory. The "today" process puts hosting in a separate folder altogether.
Please update your rule to version 2 as the error message says.
Similar question to
How do you setup local environment variables for Cloud Functions for Firebase, but specifically looking how to setup environment variables that differ between development and production.
For example,
Say I would typically have:
.env
REACT_APP_DOMAIN: https://foo-bar.web.app/
.env.development
REACT_APP_DOMAIN: http://localhost:3000/
How would one create a similar setup for Firebase Cloud Functions?
I have an answer to my own question, though I don't think it's optimal.
In the Firebase's local-emulator documentation, they have this section:
Set up functions configuration (optional)
If you're using custom functions configuration variables, first run the command to get your custom config (run this within the functions directory) in your local environment:
firebase functions:config:get > .runtimeconfig.json
If using Windows PowerShell, replace the above with:
firebase functions:config:get | ac .runtimeconfig.json
Though poorly explained, this is just creating a json file .runtimeconfig.json, which contains your firebase config variables. The variables in this file are then used by the Firebase emulator, in place of what is stored in Firestore. This is better explained in this github repo ticket.
Now knowing this, .runtimeconfig.json can be used in a similar manner that .env.development is. Instead of using the command they suggest, you can just manually update it as you need (or use their command to pull down your firebase variables and then modify them as needed).
You access/use those local variables in the exact same way you would normally access Firebase variables, functions.config().foo.bar.
When deployed, they'll reference your variables as set on Firebase. When in development (through firebase emulator), they'll reference what you have in .runtimeconfig.json.
I have a subscription-based android app which required the used of firebase cloud functions for notification schedule.
Now I am confused about where should I create the functions directory? Should I make it in the android project directory itself or make a separate project directory for it?
Where should I create the functions directory?
You should create it as a sub-folder (or sub-directory) of your Firebase project folder. This way you will be able to deploy it with firebase deploy ....
If you didn't choose to add Cloud Functions when you created your Firebase project, you can run firebase init functions in the terminal under your Firebase project main folder.
More info here.
I experimented with firebase functions for the init part and it has created a folder for me. Now when I deploy each time it also picks up the functions folder. When I remove the functions folder I get an error, when deploying. How do I deploy everything but functions?
firebase deploy has a parameter --except . So to deploy everything except functions you can run:
firebase deploy --except functions
You can use the only option/flag, for example firebase deploy --only hosting,storage, see the CLI doc.
You can also use deploy targets which "are short-name identifiers (that you define yourself) for Firebase resources in your Firebase project".
Check to make sure that you have removed the implicit reference to the function in your index.js and then running firebase deploy should do the trick. If you want to explicitly delete the function completely, you can use the firebase functions:delete <myFunction> and then delete the functions folder. You can find more details in the doc here
When I first setup my firebase project and ran firebase init I didn't think i'd host a site. So I didn't check the hosting on setup.
And so i have an ios project with the following folder structure:
myproject:
- ios (xcode code)
- firebase
- functions
- index.js (i have cloud functions already deployed from here)
If I run firebase init again can i just select "hosting" so that a public folder is added without overwriting everything in the functions folder?
You can tell the CLI to only initialize hosting with:
firebase init hosting
That way your other settings will remain unmodified.