1

I have three cloud functions, two of which have been deployed to my firebase project, one of which I've just added. I was hoping to test the new one locally before deploying, but when I try to use it I'm unable, and only the two already-deployed functions are useable.

Firebase emulator runs just fine on port 5001, as per usual. I followed these instructions and added firebase.functions().useEmulator("localhost", 5001); to the func I wanted to test locally:

already-deployed func example:

// index.js
exports.addMessage = functions.region("europe-west1").https.onRequest(...

not-yet-deployed func:

// index.js
exports.testFunction = functions.useEmulator("localhost", 5001).https.onRequest(...

Then I went to https://localhost:5001/{projectId}/us-central1/testFunction, but the browser displayed a message saying only addMessage and my other deployed func was available for use. I also tried http, as was recommended in another thread.

Is there a step I'm missing here? Some kind of "local" deployment? I'm aware there's been some changes in firebase versions which could be messing things up, but I'm using the default imports/dependencies firebase installs when you run firebase init:

// package.json
"dependencies": {
  "firebase-admin": "^9.8.0",
  "firebase-functions": "^3.14.1"
},

//index.js
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
crevulus
  • 1,658
  • 12
  • 42
  • 1
    can you concise what are you trying to achieve and issue you are facing? – Divyani Yadav Sep 14 '21 at 12:07
  • @DivyaniYadav Trying to run my functions locally via the emulator so I can test them first before deploying, rather than deploying to firebase, trying them, debugging them, deploying again etc. – crevulus Sep 14 '21 at 13:57
  • 1
    can you check this link: https://stackoverflow.com/questions/56158010/firestore-firebase-emulator-not-running is it helpful? – Divyani Yadav Sep 15 '21 at 11:27
  • 1
    @DivyaniYadav I think it could be. I'd disregarded the thread, because my emulator has always been working and that thread is from a user who couldn't load an emulator at all. But I ran `firebase setup:emulators:firestore` and now my newly-added function works. Thank you. – crevulus Sep 15 '21 at 13:48
  • 1
    Updated my answer as directed by you. – Divyani Yadav Nov 05 '21 at 12:33

3 Answers3

2

UPDATE:

1: Deploy your function in the firebase in order to get recognized. you can refer to the Documentation using firebase deploy --only functions

2: Make sure that the emulator is installed by the following command: firebase setup:emulators:firestore. for this you can refer Documentation as mentioned :

The emulators:start command will start emulators for Cloud Functions, Cloud Firestore, Realtime Database, and Firebase Hosting based on the products you have initialized in your local project using firebase init. If you want to start a particular emulator, use the --only flag.

For further reference you can follow up the stackoverflow thread 1 and 2 where similar issue has been raised by the OP which might be helpful.

Divyani Yadav
  • 1,030
  • 4
  • 9
2

What you need to do is npm run build in your functions folder. Editing your functions doesn't trigger a build automatically and doesn't recognize your new function. The reason that deploying works, is that it does trigger a build.

Loolooii
  • 8,588
  • 14
  • 66
  • 90
  • Upvoted! This is correct answer, because the accepted anwer will not working in case of updation of existing cloud function. I was struggling with this from 2 hours. – Ravinder Kumar Aug 14 '23 at 14:12
1

EDIT, new answer:

Turns out you need to deploy the function to firebase once for the function to be recognised by the emulator.

Run firebase deploy --only functions and then emulate your db and you should see your function.

Previous answer: After running firebase setup:emulators:firestore my newly-added function seems to work in the emulator.

crevulus
  • 1,658
  • 12
  • 42