I am trying to test my cloud functions locally. I have a custom functions configuration variable, so I used the command firebase functions:config:get > .runtimeconfig.json to generate a runtimeconfig.json file:
{
"stripe": {
"testkey": "someKey"
}
}
Then I use the command firebase functions:shell to start the emulator, but it prints the following error in my terminal:
+ functions: Emulator started at http://localhost:5001
! TypeError: Cannot read property 'testkey' of undefined
at Object.<anonymous> (C:\Users\Jesper\intergun\functions\lib\index.js:14:52)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:532:33
at Generator.next (<anonymous>)
! We were unable to load your functions code. (see above)
- It appears your code is written in Typescript, which must be compiled before emulation.
- You may be able to run "npm run build" in your functions directory to resolve this.
For some reason it cannot find the variable, and it also seems like it thinks the code is written in TypeScript, even though it is pointing to the generated index.js file. I tried running npm run build but the result is the same.
The top of my index.ts looks like this:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import 'firebase-functions';
import * as Stripe from 'stripe';
admin.initializeApp();
const stripe = new Stripe(functions.config().stripe.testkey);
What am I missing?
I found the solution here: https://github.com/firebase/firebase-tools/issues/711
To generate .runtimeconfig.json, I used the command firebase functions:config:get > .runtimeconfig.json. However, this generated a file which seemed to be correct but apparently had invisible characters that shouldn't be there. Instead, I used this command: firebase functions:config:get | ac .runtimeconfig.json. This solved the problem.
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?
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.
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)
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