as you all aware about firebase CLI's new feature which is local emulators explained here(https://firebase.googleblog.com/2020/05/local-firebase-emulator-ui.html?m=1)
so I have updated my CLI and when I run firebase emulators:start I get this error in my functions code
kishan#kishans-Air functions % firebase emulators:start
i emulators: Starting emulators: functions
⚠ Your requested "node" version "8" doesn't match your global version "10"
⚠ hosting: The hosting emulator is configured but there is no hosting configuration. Have you run firebase init hosting?
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "/Users/kishan/Desktop/dilip/googlecloudfunctions/functions" for Cloud Functions...
⚠ TypeError: Cannot convert object to primitive value
at Proxy.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:311:83)
at Object.<anonymous> (/Users/kishan/Desktop/dilip/googlecloudfunctions/functions/lib/index.js:7:7)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:661:33
⚠ We were unable to load your functions code. (see above)
- It appears your code is written in Typescript, which must be compiled before emulation.
can anyone help what I need to do here ??
The error message is suggesting to you:
It appears your code is written in Typescript, which must be compiled before emulation.
You have to build the code first with npm run build. The emulator will not do that for you.
I found that instead of calling:
admin.initializeApp(functions.config().firebase)
I had to instead call:
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
This resolved the issue.
Related
I'm trying to build a CI/CD pipeline for my project that uses Firebase Cloud Functions.
The functions code is written in TypeScript.
Deploying the code from my local MacBook works perfectly. However, when deploying the same exact code from the GitHub Action runner it fails with an error:
/home/runner/work/geonotes-backend/geonotes-backend/functions/lib/handlers/createNoteActionHandler.js:11
return sendOutput({ note_id: note?.id });
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:1052:16)
at Module._compile (internal/modules/cjs/loader.js:1100:27)
at Module._compile (pkg/prelude/bootstrap.js:1394:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:10)
at Module.load (internal/modules/cjs/loader.js:984:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1024:19)
at Module.require (pkg/prelude/bootstrap.js:1338:31)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/runner/work/geonotes-backend/geonotes-backend/functions/lib/index.js:28:51)
This looks like Firebase is not accepting the optional chaining syntax, but it should since the function runs on Node.js 14.
Furthermore, the same syntax is accepted perfectly by the CLI running on my machine.
I have verified that both my machine and the GitHub runner are using the same versions:
Node.js v14.17.4
NPM v6.14.14
Firebase CLI v9.16.0
TypeScript v4.3.5
I have also tried to run my action on macos-latest instead of ubuntu-latest with no luck.
This is one of the runs that is failing, with --debug enabled:
https://github.com/emilioschepis/geonotes-backend/runs/3261106553
Does anyone have an idea about what could be wrong?
I'm creating an app that uses firebase-functions. I created my app directory and ran firebase init. After that was completed I tried to run the hello world program that it gives you using firebase serve, and I get this error:
TypeError: _onRequestWithOpts is not a function
at Object.httpsProvider._onRequestWithOpts
from what it looks like, it seems to be an issue with the onRequest() method but I have made apps using this before and have never ran into this issue. I literally didn't change a single piece of code that they initially give you so unless they decided to change something overnight i'm not sure what the issue is.
using firebase deploy works, but firebase serve does not. Using express and adding new functions, the error pops up when running firebase deploy and serve.
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-
functions
//
exports.helloWorld = functions.https.onRequest((request, response)
=> {
response.send("Hello from Firebase!");
});
Gabriels-MacBook-Pro:functions Mike$ firebase serve
=== Serving from '/Users/Gabe/Desktop/mycart-functions'...
⚠ Your requested "node" version "8" doesn't match your global
version "10"
✔ functions: Emulator started at http://localhost:5000
i functions: Watching "/Users/Gabe/Desktop/mycart-
functions/functions" for Cloud Functions...
⚠ TypeError: _onRequestWithOpts is not a function
at Object.httpsProvider._onRequestWithOpts
(/usr/local/lib/node_modules/firebase-
tools/lib/emulator/functionsEmulatorRuntime.js:278:24)
at Object.httpsProvider.onRequest
(/usr/local/lib/node_modules/firebase-
tools/lib/emulator/functionsEmulatorRuntime.js:283:34)
at Object.<anonymous> (/Users/Gabe/Desktop/mycart-
functions/functions/index.js:6:39)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
⚠ We were unable to load your functions code. (see above)
Goal
Use the firebase emulator and deploy firebase cloud functions locally for test and debug.
Issue
When firebase serve is run, it fails with - Error: Certificate object must be an object.
What's been tried
Following Google's documentation here, the instructions include
these steps:
To set up admin credentials for emulated functions (other than Cloud
Firestore and Realtime Database):
Open the Service Accounts pane of the Google Cloud Console.
Make sure that App Engine default service account is selected, and use the options menu at right to select Create key.
When prompted, select JSON
for the key type, and click Create. Set your Google default
credentials to point to the downloaded key:
$ export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"
$ firebase functions:shell
OR
$ firebase serve --only functions
Completing these instructions and running firebase serve... fails. Here are the logs:
Silver-Sliver:functions dchaddportwine$ firebase serve --only functions
=== Serving from '/Users/dchaddportwine/Sites/people-is-cloud-functions/functions'...
i functions: Preparing to emulate functions.
Warning: You're using Node.js v8.11.1 but Google Cloud Functions only supports v6.11.5.
⚠ functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
⚠ functions: Error from emulator. Error occurred while parsing your function triggers.
Error: Certificate object must be an object.
at FirebaseAppError.FirebaseError [as constructor] (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseAppError (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/utils/error.js:119:28)
at new Certificate (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/auth/credential.js:106:19)
at new CertCredential (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/auth/credential.js:189:64)
at Object.cert (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/firebase-namespace.js:220:58)
at Object.<anonymous> (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/index.js:21:32)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
Question
How do I fix these two errors?
I have firebase-functions installed, and have tried npm i --save firebase-functions
⚠ functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
Thinking this has to do with GOOGLE_APPLICATION_CREDENTIALS, but I'm not sure how to verify or check what's happening here.
⚠ functions: Error from emulator. Error occurred while parsing your function triggers.
Error: Certificate object must be an object.
UPDATE
Further along in the documentation, it reads:
If you're using custom functions configuration variables, run the
following command in the functions directory of your project before
running firebase serve.
firebase functions:config:get > .runtimeconfig.json
After running this command, I no longer get the certificate error. Woot! However, I am getting new failures and not many hints from the logs.
=== Serving from '/Users/dchaddportwine/Sites/people-is-cloud-functions/functions'...
i functions: Preparing to emulate functions.
i hosting[development-is]: Serving hosting files from: ./
✔ hosting[development-is]: Local server: http://localhost:5000
Warning: You're using Node.js v8.11.1 but Google Cloud Functions only supports v6.11.5.
⚠ functions: Failed to emulate app
⚠ functions: Failed to emulate helloSlack
⚠ functions: Failed to emulate helloIssy
⚠ functions: Failed to emulate interactiveIssy
i functions: No HTTPS functions found. Use firebase functions:shell if you would like to emulate other types of functions.
Code
Here is the firebase cloud functions for helloSlack
exports.helloSlack = functions.https.onRequest((req, res) => {
if (req) {
res.status(200).send(req.body);
} else {
console.log("req Error...");
throw res.status(500);
}
});
Answer
Further along in the documentation, it reads:
If you're using custom functions configuration variables, run the
following command in the functions directory of your project before
running firebase serve.
firebase functions:config:get > .runtimeconfig.json
This project is using custom functions configuration variables. So, after running the config command, I no longer get the certificate error. Woot!
I'm testing firebase functions locally in my existing project in typescript. When i run following command, i always get typeerror even though i can upload it to firebase project. the error always occured after typescript compile to js
firebase serve --only functions
TypeError: Cannot read property 'username' of undefined
at Object. (C:\Users\phone\Desktop\VMS\mynewvm_functions\functions\lib\email\index.js:8:52)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object. (C:\Users\phone\Desktop\VMS\mynewvm_functions\functions\lib\index.js:19:15)
at Module._compile (module.js:635:30)
import * as functions from 'firebase-functions';
import * as nodemailer from 'nodemailer';
const accountname = functions.config().emailaccount.username;
const accountpassword = functions.config().emailaccount.password;
/* smtp configuration */
I already set emailaccount in my project, below is my code.
firebase functions:config:set emailaccount.username="email#domain.com"
emailaccount.password="mypassword"
Please note that it is working fine when uploaded to firebase functions but it's not when serving locally. what can i do to make it work locally?
Finally, I just replace all my credentials with actual values instead of getting from firebase config. And I cd to functions folder and type 'npm start'. it look like when i run 'firebase serve --only functions', it doesn't apply with recent changes so give me the same error until I type 'npm start' which will run tsc and firebase shell, stop it (ctrl+c) then type 'firebase serve --only functions' again. Fyi, I created my current firebase functions project using firebase cli and typescript.
firebase cli - 3.18.4
firebase functions - ^1.0.1
firebase admin - 5.12.0
#google-cloud/storage - 1.6
After testing for long period of time, I decided to put username, password and tokens directly in my functions for local testing although I don't really like this idea and I change back to functions.config().. when i deploy to firebase. Thanks for #Frank for answers
There is no built-in config parameter called emailaccount, so it seems you defined it yourself.
The local environment does not automatically contain these variables. You will actually have to define them in a file called runtimeconfig.json. An easy way to do that is shown here:
cd functions
firebase functions:config:get > .runtimeconfig.json
I have a Firebase project that I've created earlier this year. It uses Cloud Functions to run some operations on the Realtime Database.
Yesterday, I learned about the Callable Cloud Functions, so I decided to try it in my app to see if I should update my existing Functions or not. I've created a simple Cloud Function for testing purposes:
exports.testCall = functions.https.onCall((data, context) =>{
console.log(context.auth.uid);
return {response:"This means success"};
});
But when I try to deploy this function, I get the error:
Error: Error occurred while parsing your function triggers.
TypeError: functions.https.onCall is not a function
at Object. (/tmp/fbfn_7614ijhDwX0NY98S/index.js:114:36)
at Module._compile (module.js:649:30)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
at Object. (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:32:3)
I'm aware that Callable Cloud Functions has been introduced in the latest Firebase SDK, so I've tried updating it using:
sudo npm install -g firebase-tools
But I can't yet deploy my Cloud Function. I've also tried a partial deploy, as shown in the Firebase docs, but it didn't work. Is there something I'm missing in the documentation?
Simply updating your Firebase CLI won't solve the problem because you must also update the Cloud Functions SDK in the Project Directory. You mentioned the Firebase Project has been initialized earlier this year, so that's before the release of Callable Cloud Functions.
See, when you init Cloud Functions in your Firebase Project, it creates the functions directory which contains the Cloud Functions SDK. So you (probably) still have the old SDK in the functions directory and you need to update it. To do that, navigate to that directory and run the command:
sudo npm i --save firebase-functions#latest