I have an exception when deploying a Firebase function. Has anyone run into this weird error? I am confused by the error message.
If so, what is the solution?
I am using the below command
firebase deploy --only functions:chapterpptx
i deploying functions
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i functions: ensuring required API artifactregistry.googleapis.com is enabled...
✔ functions: required API artifactregistry.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
Error: Error occurred while parsing your function triggers.
/Users/xxxxxx/Projects/nationals/functions/node_modules/firebase-admin/lib/app/firebase-app.js:174
this.appStore?.removeApp(this.name);
^
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)
Here is my package.json for the Firebase functions folder
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"dependencies": {
"axios": "^1.1.3",
"cors": "^2.8.5",
"firebase": "^9.12.1",
"firebase-admin": "^11.2.0",
"firebase-functions": "^4.0.1",
"xml-js": "^1.6.11"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}
Related
I already have a firebase functions project setup in some folder in my computer but was trying to set it up somewhere else. When doing that, it happens to mess up something somewhere and i now get that error when deploying my functions using firebase deploy :
functions# build C:\Users\bertr\Documents\GitHub\GreenplayFirebaseFunctions\functions
> tsc
+ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
+ functions: required API cloudbuild.googleapis.com is enabled
+ functions: required API cloudfunctions.googleapis.com is enabled
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'SDK_VERSION' of undefined
at registerDatabase (C:\Users\bertr\Documents\GitHub\GreenplayFirebaseFunctions\functions\node_modules\#firebase\database\dist\index.node.cjs.js:15610:28)
at Object.<anonymous> (C:\Users\bertr\Documents\GitHub\GreenplayFirebaseFunctions\functions\node_modules\#firebase\database\dist\index.node.cjs.js:15645:5)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at FirebaseNamespace.get (C:\Users\bertr\Documents\GitHub\GreenplayFirebaseFunctions\functions\node_modules\firebase-admin\lib\firebase-namespace.js:259:38)
at Object.<anonymous> (C:\Users\bertr\Documents\GitHub\GreenplayFirebaseFunctions\functions\lib\firebase\firebase.js:13:20)
Tried to upgrade firebase-tools, reinstalling firebase, setup account again but i still get that error. Maybe that has a link to firebase SDK V8 and 9 but i'm not sure... I did'nt change anything in my code.
here's my firebase.json file :
{
"functions": {
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build",
"source": "functions"
},
"emulators": {
"functions": {
"port": 8201
},
"firestore": {
"port": 9288
},
"database": {
"port": 6120
},
"ui": {
"enabled": true
}
}
}
and my package.json file :
{
"name": "functions",
"scripts": {
"build": "tsc",
"serve": "npm run build && firebase emulators:start --import ../../database",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "npm run build && firebase deploy --only functions",
"logs": "firebase functions:log",
"test": "mocha -r ts-node/register tests/**/*.test.ts"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.4.2",
"firebase-functions": "^3.13.0",
"geofire-common": "^5.2.0",
"geolib": "^3.3.1",
"moment-timezone": "^0.5.32",
"tmp": "^0.2.1",
"xlsx": "^0.17.2"
},
"devDependencies": {
"#types/chai": "^4.2.18",
"#types/mocha": "^8.2.2",
"#types/tmp": "^0.2.1",
"chai": "^4.3.4",
"firebase-functions-test": "^0.2.0",
"mocha": "^9.0.0",
"ts-node": "^9.0.0",
"typescript": "^3.8.0"
},
"private": true
}
Thanks a lot guys!
Turns out i needed to update firebase-admin to latest version inside functions folder..
npm i firebase-admin
works for me!
I want to use Stripe with Firebase Functions.
Attempting to deploy the following source code to functions will result in an error.
const functions = require('firebase-functions');
const express = require("express");
const app = express();
// This is your real test secret API key.
const stripe = require("stripe")("STRIPE KEY HERE");
app.use(express.static("."));
app.use(express.json());
const calculateOrderAmount = items => {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
};
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd"
});
res.send({
clientSecret: paymentIntent.client_secret
});
});
module.exports.express = functions.https.onRequest(app);
This is what I want to achieve.
https://stripe.com/docs/payments/integration-builder
I want to deploy to Firebase Functions using react for the front and Node for the back end.
</functions/package.json>
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "12"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
When I tried the command "firebase deploy --only" functions: express ", I got the following error:
Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging Function cannot be initialized
This was accompanied by this Log object:
{
"#type": "type.googleapis.com/google.cloud.audit.AuditLog",
"status": {
"code": 3,
"message": "Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation."
},
"authenticationInfo": {
"principalEmail": "MYADDRESS#gmail.com"
},
"serviceName": "cloudfunctions.googleapis.com",
"methodName": "google.cloud.functions.v1.CloudFunctionsService.UpdateFunction",
"resourceName": "projects/MYPROJECT/locations/us-central1/functions/express"
}
i deploying functions
i functions: ensuring required API cloudfunctions.googleapis.com is enabled ...
i functions: ensuring required API cloudbuild.googleapis.com is enabled ...
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading ...
i functions: packaged functions (44.09 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: creating Node.js 12 function express (us-central1) ...
Functions deploy had errors with the following functions:
express (us-central1)
To try redeploying those functions, run:
firebase deploy --only "functions: express"
To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
Does anyone know the solution?
You need to export the Express.js app as an HTTPS function with
exports.app = functions.https.onRequest(app);
instead of doing
module.exports.express = functions.https.onRequest(app);
See the doc.
Then you need to deploy with, for example, firebase deploy --only functions.
Note that it seems that there is an extra double quote (") in the command you entered:
I typed the command "firebase deploy --only" functions: express "
I recently getting this error when try to deploy the firebase cloud functions using the command firebase deploy --only functions. My package.json file is shown below
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.6.2",
"stripe": "^8.50.0"
},
"devDependencies": {
"#types/cors": "^2.8.6",
"dotenv": "^8.2.0",
"firebase-functions-test": "^0.2.0",
"tslint": "^5.12.0",
"typescript": "^3.8.0"
},
"private": true
}
Using npm package firebase-tools of version 8.4.1.
Also getting error in firebase emulators
Enter into functions directory
enter the following command npm run build
and then deploy / launch your emulators
To me what happened was that I added a test folder out side of src folder. Because I use typescript the typesctipt compiler lost it and changed the output directory.
So if that's the case, move the other folders inside src.
I have defined the following function in my index.ts file:
export const stripeCharge = functions.region('europe-west1').database
.ref('/payments/{userId}/{paymentId}')
.onWrite(async (change, context) => {
...
});
I want to debug this function, so I am trying to use the Google Cloud Functions Emulator (npm install -g #google-cloud/functions-emulator).
First i run:
functions start
to start the emulator.
Then I want to deploy the function:
functions deploy --trigger-http --timeout 600s stripeCharge
This results in the following errors:
ERROR: Function load error: Code could not be loaded.
ERROR: Does the file exists? Is there a syntax error in your code?
ERROR: Detailed stack trace: Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
C:\Users\Jesper\intergun\functions\lib\index.js:14
const stripe = new Stripe(functions.config().stripe.testkey);
^
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 [eval]:1:40
at ContextifyScript.Script.runInThisContext (vm.js:50:33)
ERROR: Error: Failed to deploy function.
at exec (C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\#google-cloud\functions-emulator\src\cli\controller.js:126:22)
at ChildProcess.exithandler (child_process.js:288:5)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:915:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
So i think the problem is that it cannot find FIREBASE_CONFIG and GCLOUD_PROJECT, which I don't understand since they should be automatically populated according to this: https://firebase.google.com/docs/functions/config-env
This is at the top of my index.ts file:
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);
I also have a .runtimeconfig.json file which contains the following:
{
"stripe": {
"testkey": "..."
}
}
Finally, here is my package.json:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "lib/index.js",
"dependencies": {
"#google-cloud/storage": "^2.5.0",
"firebase-admin": "^8.1.0",
"firebase-functions": "^3.0.1",
"stripe": "^7.1.0"
},
"devDependencies": {
"#types/sharp": "^0.22.2",
"#types/stripe": "^6.30.0",
"firebase-functions-test": "^0.1.6",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
How can I fix this?
You use the emulators provided by the Firebase CLI in order to test functions written using the Firebase SDKs. The Cloud emulator (#google-cloud/functions-emulator) will not work.
Unfortunately, you're writing a Realtime Database function, which is currently not supported by the emulator.
I am trying to test my cloud functions locally. I am using the command firebase functions:shell which starts the emulator successfully. I have the following cloud function in my index.ts:
export const stripeCharge = functions.region('europe-west1').database
.ref('/payments/{userId}/{paymentId}')
.onWrite(async (change, context) => {
someCode;
});
I read that you should invoke an onWrite firestore function the following way (https://firebase.google.com/docs/functions/local-shell):
stripeCharge({before: "oldData", after: "newData"})
However, this results in the following error:
'Successfully invoked function.'
firebase > ! TypeError: Cannot read property 'eventType' of undefined
at cloudFunction (C:\Users\Jesper\intergun\functions\node_modules\firebase-functions\lib\cloud-functions.js:80:40)
at Run (C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:458:20)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:442:19
at Generator.next (<anonymous>)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71
at new Promise (<anonymous>)
at __awaiter (C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)
at Run (C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:435:12)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:457:15
at Generator.next (<anonymous>)
! Your function was killed because it raised an unhandled error.
The error happens in some other file and I'm not sure why. What am I missing?
Edit: My package.json:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "lib/index.js",
"dependencies": {
"#google-cloud/storage": "^2.5.0",
"#types/fs-extra": "^7.0.0",
"firebase-admin": "^8.1.0",
"firebase-functions": "^3.0.1",
"fs-extra": "^8.0.1",
"mailgun-js": "^0.22.0",
"sharp": "^0.22.1",
"stripe": "^7.1.0"
},
"devDependencies": {
"#types/sharp": "^0.22.2",
"#types/stripe": "^6.30.0",
"firebase-functions-test": "^0.1.6",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
After replicating this I've found this thread it looks there is an open case for this issue.
The similar setup works fine for Firestore, but the issue seems to to be with the RTDB, I assume we have to wait until they solve it, sorry :(