I have a conflict between my node version and firebase-functions - node.js

I installed the latest version of firebase-functions, which is 4.2.1 and I'm running node version 19.6.0; however, when I set my node version in the package.json file, I'm getting an error when trying to run the firebase emulator that it wants version 10, 12, 14, or 16. The error says: "functions: Failed to load function definition from source: FirebaseError: package.json in functions directory has an engines field which is unsupported. Valid choices are: {"node": 10|12|14|16}"
This is my package.json file:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"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": "19"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^11.5.0",
"firebase-functions": "^4.2.1"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}
I've tried changing my node version in the package.json file to 16; however, I then get an error that says it was expecting node version 16 but got node version 19.6.0. So I'm stuck at this point.

Related

Use Node-Fetch in TypeScript with CloudFunctions

So Im using Cloud Functions from Firebase with TypeScript
I want to import node-fetch like:
import * as fetch from "node-fetch";
but it doesn't import correctly:
when I try to use fetch like this:
fetch("https://payments.sandbox.braintree-api.com/graphql", {
method: "POST",
headers: {
"Authorization":
"bnIzdm5nZHlqempjNnQ3bTo0ZjMxYjQ5YjA2MDNjN2RkMjZhM2UyMGE3M2E3MWVlNw==",
"Braintree-Version": "2022-08-13",
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
}),
})
I get following problem:
This expression is not callable.
Type 'typeof import(".../node_modules/node-fetch/#types/index")' has no call signatures.
I was wondering whether I need to add a dependency after running:
npm install node-fetch
because in my package.json
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"build:watch": "tsc --watch",
"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": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.18.0"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^5.12.0",
"#typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^0.2.0",
"typescript": "^4.5.4"
},
"private": true
}
there is no node-fetch dependency
if this is the case how can I fix my problem?
As mentioned here, you have to add this dependency explicitly in the package.json, as node-fetch is not a built-in module and will need installation.
You can run npm install node-fetch on your local machine, but the Cloud Functions deploy doesn't work this way.
HTH, else let me know.

Can't update firebase function to node 12 in Angular Universal app with AngularFire

I followed this tutorial to deploy Angular Universal to a firebase function and deploy with Firebase: https://medium.com/#chris.duebbert/angular-9-universal-firebase-deploy-8d8d47244e1c
Everything worked fine until I add AngularFireModule to my app.module.ts. Then when I run 'ng deploy' using node 12 I get the following error in the logs of my 'ssr' function:
Provided module can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: ReferenceError: globalThis is not defined
When I try with my local node version set to 10 I get the same error directly in my terminal.
Apparently, Firebase uses globalThis which requires node version 12 (Angular 11 run on SSR #nguniversal/express-engine ReferenceError: globalThis is not defined). I added the following to my /functions/package.json:
app.module.ts
"engines": {
"node": "12"
},
I can put any number for node version and my SSR function is still deploying with node 10 and the 'globalThis' error continues. I'm using Firebase's Blaze plan. Here's my app.module.ts:
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
AppRoutingModule,
NoopAnimationsModule,
FormsModule,
NgxTwitterTimelineModule,
AngularFireModule.initializeApp(config),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
functions/package.json
{
"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": "12"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.4.2",
"firebase-functions": "^3.13.0",
"grpc": "^1.24.4"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.8.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
You need to polyfill globalThis https://github.com/angular/angularfire#polyfills
Two weeks looking for a solution, until I have found this question/answer. Thank you!
Steps to solve the problem:
> npm install globalthis
on server.ts file:
import 'globalthis/auto';

Function deployment hangs at "all necessary APIs are enabled"

When deploying the Cloud Functions to the Firebase Cloud, they take a very long time right after the log "functions: all necessary APis are enabled".
I am deploying 6 functions.
I am using node 10 since using node 8 in combination with the URL module caused some errors that went away using node 10.
the package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"babel-eslint": "^10.0.1",
"cheerio": "^1.0.0-rc.2",
"firebase-admin": "~7.0.0",
"firebase-functions": "^2.3.0",
"lighthouse": "^5.1.0",
"ping": "^0.2.2",
"puppeteer": "^1.17.0",
"puppeteer-extra": "^2.1.3",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"url": "^0.11.0"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1"
},
"engines": {
"node": "10"
},
"private": true
}
The deployment used to go much faster, but recently it started behaving differently.
I dont know when excactly it happened.
My questions are:
Does anyone know what the issue might be?
If no, does anyone know how I would go about 'debugging' this issue?
Thank you.

Using a Firestore Cloud Functions

I have this problem. In my code depends of choosen project name I want to retrieve a document from another collection. I am use
exports.updateUser = functions.firestore
.document('Users/{userId}')
.onUpdate((change, context) => {
const newValue = change.after.data().currentProject;
console.log("Project changed to - " + newValue);
const previousValue = change.before.data().currentProject;
console.log("from - " + previousValue);
// ### FROM HERE DOES NOT WORK ###
const projectId = firestore.collection('Projects')
.where('name', '==', newValue).get()
.then(doc => {
console.log(doc.data().id);
return doc.data().id;
});
});
And I get an error
ReferenceError: firestore is not defined
at exports.updateUser.functions.firestore.document.onUpdate (/user_code/index.js:25:14)
So maybe I need another dependencies ?
My package.json is
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
What I am missing? Please give a hint
You will need to use the Firebase Admin SDK to access Firestore collections that are outside of the scope that your function was triggered on. Typically this will look something like:
admin.firestore().collection('Projects')...

Firebase not deploying from command line in mac

I've been trying to deploy my firebase functions project for some time now and I can't seem to do it. I've followed some other question suggestions but none have worked. I've tried adding escaped colons to the predeploy:
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\""
]
And I've also tried renaming my spaced folder into a single word folder and it hasn't worked either. This is the error:
Error: functions predeploy error: Command terminated with non-zero exit code1
This is my package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "^5.8.2",
"firebase-functions": "^1.0.1"
},
"private": true
}
And this is my firebase.json:
{
"database": {
"rules": "database.rules.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\""
]
},
"storage": {
"rules": "storage.rules"
}
}
Note: I removed the run lint myself from the predeploy.
I can't even seem to deploy the helloWorld example function. When trying to serve the project I'm getting this warning:
Warning: You're using Node.js v9.4.0 but Google Cloud Functions only supports v6.11.5
However, the project is actually served correctly. I've also tried uninstalling the google-cloud globally and installing it again, and the warning didn't go away.
Please help me, I've run out of ideas. Thank you in advance.
You either remove the npm --prefix or add at least one
{
"functions": {
"predeploy": [],
"source": "functions"
}
}
Or you try it with this:
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
}
}

Resources