Can not see the Firebase function deployed - firebase

I followed the following steps:
The Firebase CLI (Command Line Interface) requires Node.js and npm, which you can install by following the instructions on https://nodejs.org/
Installing Node.js also installs npm
Once you have Node.js and npm installed, install the Firebase CLI via npm:
npm install -g firebase-tools
This installs the globally available firebase command. To update to the latest version, re-run the same command
Initialize your project:
a. Run firebase login to log in via the browser and authenticate the firebase tool.
b.Go to your Firebase project directory or create the directory
c. Run firebase init functions
The tool gives you an option to install dependencies with npm. It is safe to decline if you want to manage dependencies in another way.
Select associated firebase project
Select Y to install dependencies with npm
Move to directory setup firebase functions
Edit index.js file with the function you created
Run the firebase use --add to add your Firebase project
Run firebase deploy --only functions to deploy the function
After all this I get the message in the terminal at deploy was completed but in the Firebase console, when i click on Functions tab there are no functions listed!?

Quick Tip: Make sure you are exporting the function you are trying to deploy in your index.js file. Your firebase project will deploy but Cloud Functions won't be available unless they are exported.

Make sure you save the file after uncommenting the default function and then use
firebase deploy

For Cloud Functions, it is required to add your function to the special exports object (it is a Node's way of making the function accessible outside of the current file)
Make sure to have index.js in your functions directory:
Example of a function:
// Import the Firebase SDK for Google Cloud Functions.
const functions = require('firebase-functions');
// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp();
// Your function declaration. Example of a fired function when a user is created in Auth feature.
exports.myFunction = functions.auth.user().onCreate(async (user) => {
// ... your code here.
});
Then for deployment follow these steps:
First, if not done, make sure to have firebase-tools installed:
$ npm install -g firebase-tools
And initialised: $ firebase init
For full deployment:
$ firebase deploy
OR for functions deployment
$ firebase deploy --only functions
OR to deploy specific functions
$ firebase deploy --only functions:function1,functions:function2
A good read with a very useful example: https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#7

I went through the same issue recently, while performing Actions on Google Node.js Client Library Version 1 Migration Guide. to Node.js Client Library V2 (That I highly recommend) It took me a while to figure out what what was happening. At the I couldn't really figure out what the problem was! So here is what I did and it worked for me:
Make sure you have a backup copy of your cloud functions (index.js) and maybe your package.json (Just in case you don't want to remember what packages you previously had installed - Could be annoying sometimes).
Delete the entire functions directory from your project folder.
Re-launch firebase CLI with firebase init and choose Functions
Once your cloud function have been initialized, CD into the functions folder and Redeploy it using firebase deploy --only functions.
If everything goes well 😃, you should now see your function deployed on your firebase dashboard console.
N.B: Google recently released the Node.js Client Library version 2 (v2) in April 16th 2018 with a lot of new features. After April 16th, 2018, new features on Actions on Google will no longer be added to v1 of the client library. If you want to use new features, you must migrate to v2 client library.
In addition, the v1 client library does not support Dialogflow v2. If you need Dialogflow v2 functionality, you’ll also need to migrate to v2 of the client library.
Hope this helps 👍.

In step 7, you have to uncomment the sample function in there and save the file. Then, in the output of the deploy command, you will be given a url for the created helloWorld function.

I had exactly the same problem and I solved it by making sure the index.js file containing all my functions was saved on the "functions" folder inside the project folder. I am using vs code so I just clicked on file/save as and selected the correct folder.

#Learn2Code
I had the exact same issue.
Ensure that in your index.js file, you export your function before initializing your app.
Now, go ahead and run firebase deploy from your project directory.
For example:
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
const snapshot = await admin.database().ref('/messages').push({original: original});
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref.toString());
});
const admin = require('firebase-admin');
admin.initializeApp();

Make sure you're running at least version 3.5.0 of firebase-tools. To check which version you have, run:
firebase --version
If you're running the default setup, you can update firebase-tools using:
npm install -g firebase-tools

Had the same situation. The problem was that when I was doing
$ firebase deploy --only "myFunction"
That filter name: myFunction, was not exactly the same as the name of the function I was trying to deploy. Silly mistake but took me a day to realize...

To clarify one issue - it appears as though your index.js file inside the functions folder must export functions created within the same file (similar to what Fran had said).
It seems trying to organize your files into subfolders will not work properly with Firebase functions - same rules apply for using firebase serve to test locally (must create codeinside functions/index.js).
Hope this helps someone!

1) Make sure you are exporting the function you are trying to deploy in your index.js file.
2) Write 'use-strict' at the top of your file (index.js) then use console to deploy your function

Check your "default project" at firebase init. Select one with similar name was my mistake. ;)

Use firebase projects:list and firebase use <project> to make sure the Firebase CLI's "current project" is set correctly regardless of what folder you're in.
Example:
> firebase projects:list
✔ Preparing the list of your Firebase projects
┌──────────────────────┬─────────────────────┬──────────────────────┐
│ Project Display Name │ Project ID │ Resource Location ID │
├──────────────────────┼─────────────────────┼──────────────────────┤
│ alpha │ alpha (current) │ [Not specified] │
├──────────────────────┼─────────────────────┼──────────────────────┤
│ beta │ beta │ [Not specified] │
└──────────────────────┴─────────────────────┴──────────────────────┘
2 project(s) total.
> firebase use beta
Now using project beta

I had this error as well. I had copied a working function running on Google Cloud Functions from a previous project and could not figure out why it would not show up once deployed.
I needed to wrap my function in functions.https.onRequest(), which is not required on normal cloud functions.

Had the same issue.
In my case solved by using firebase deploy wihtout any --only, which revealed better error messaging and I had to setup billing in Cloud Console for the project. The setup routine can be triggered by selecting Could Functions in the Console.
I had another issue in which the Service Account was missing from the Project, so I setup a fresh project through the Console first and then added this to Firebase.

Make sure you init firebase on step back from your firebase functions. and also firebase functions name must be functions.
It works for me

One dumb gotcha I just ran into, I was running "firebase deploy" from the top level folder (one above the /functions sub-folder), and it deployed "successfully" but I never saw it in Firebase itself. It wasn't until I cd'd into /functions and re-ran firebase deploy that the full deployment actually worked.

Related

firebase 'run payments with stripe' extension not creating the cloud functions in index.ts (firestore emulator suite)

im trying to set up the stripe extension for firebase localy with the emulator suite, i followed the steps here: https://firebase.google.com/products/extensions/stripe-firestore-stripe-payments after step 3 an extension folder with the file firestore-stripe-payments.env is created and in the firebase.json file the extension gets listed,
created/changed files by the extension
but thats all what was created, from my understanding some
cloud functions regarding stripe should be created by the stripe extension(inside the index.ts file)
so in step 4: to Test this extension locally with '$ firebase emulators:start' i dont know how/where i can see these functions or test them.
also i dont see any collection regarding stripe in firestore(i already created a product in stripe dashboard), it should look like this image
ps: no error is thrown while execution all steps (i didnt do the last step for the deployment)
When you run firebase emulators:start, you should get back a link to the emulator UI. When you look at your installed extension in the emulator UI you should be able to see the functions for the extension under "APIs and resources".
For your question about there being no collection regarding Stripe - have you already created the Firestore database? It's called out as a step you need to complete before installing the extension under the "Additional setup" section.
Updating the firebase-tools to the latest version with "npm install -g firebase-tools" solved it for me, before i had v10.0.8 and only with v10.0.9 the emulator suite supports the extensions feature. https://github.com/firebase/firebase-tools/releases/tag/v10.9.0

Firebase Deploy: Error: Could not detect language for functions at

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.

Firebase disable functions

On a Vue app I enabled firebase functions to send messages from contact form.
I do not want to use functions anymore, but only hosting and firestore.
I have removed the function from index.js in functions folder, but when I do firebase deploy I get:
Error: Your project must be on the Blaze (pay-as-you-go)
plan to complete this command. Required API cloudbuild.googleapis.com
can't be enabled until the upgrade is complete.
This happened after upgrading to node 10, and it's the reason I don't want to use functions anymore.
How do I remove functions completely from project?
I was able to deploy after upgrading by removing:
"functions": {
"source": "functions"
}
in firebase.json. Optionally I also removed functions folder.
You can simply delete all the functions manually in the Firebase console.
Or, you can downgrade back to node 8 and redeploy an empty index.js to delete the functions. You will need to use a version of the CLI less than version 9.0.0.

After following all instructions for migrating to Firebase Cloud Functions 1.0, deployment fails

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.

Will running firebase init a second time overwrite node modules and cloud functions?

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.

Resources