react-most-wanted icon indicating copy to clipboard operation
react-most-wanted copied to clipboard

firebase deploy --only functions

Open Molnfront opened this issue 2 years ago • 25 comments

I tried to only depoy the functions and got this:

Error: Could not detect language for functions at /Users/.../firebase/functions

Then I tried:

firebase init functions

and got:

=== Deploying to 'noshit'...

i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint npm ERR! code ENOENT npm ERR! syscall open npm ERR! path /Users/noshit/firebase/functions/package.json npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, open '/Users/noshit/firebase/functions/package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in: npm ERR! /Users/noshit/.npm/_logs/2022-03-23T20_49_07_403Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code254

Molnfront avatar Mar 23 '22 20:03 Molnfront

Hm, I found out that init created a new functions folder in the root with the necesary package.json and node_modules folder. So I copied those except the index.js to the /firebase/functions folder.

Then tried to deploy again.

Got error agsain because of missing packages.

Had to install the following packages:

firebase-function-tools

@firebase/[email protected]

nodemailer

mkdirp-promise

child-process-promise

Now I could deploy.

11 warning about code in the function:

/firebase/functions/auth/onDelete.f.js 3:8 warning 'moment' is defined but never used no-unused-vars

/firebase/functions/db/groupChats/name/onUpdate.f.js 18:34 warning Array.prototype.map() expects a return value from arrow function array-callback-return

/firebase/functions/db/userChatMessages/onCreate.f.js 27:11 warning 'senderRef' is assigned a value but never used no-unused-vars

/firebase/functions/https/admin/onCall.f.js 9:38 warning Unexpected string concatenation of literals no-useless-concat

/firebase/functions/https/api/companies/onRequest.f.js 6:5 warning 'whitelist' is assigned a value but never used no-unused-vars

/firebase/functions/https/api/users/onRequest.f.js 6:5 warning 'whitelist' is assigned a value but never used no-unused-vars

/firebase/functions/https/messages/onCall.f.js 8:38 warning Unexpected string concatenation of literals no-useless-concat

/firebase/functions/https/usersSync/onCall.f.js 18:40 warning Unexpected string concatenation of literals no-useless-concat

/firebase/functions/storage/onFinalize.f.js 2:8 warning 'admin' is defined but never used no-unused-vars

/firebase/functions/storage/thumbnails.js 4:1 warning Unexpected default export of anonymous function import/no-anonymous-default-export

/firebase/functions/utils/splitStringToArray.js 38:12 warning 'i' is already defined no-redeclare

✖ 11 problems (0 errors, 11 warnings)

Then lots of Error

Error: There was an error deploying functions:

  • Error Failed to create function auth-onCreate in region us-central1
  • Error Failed to create function db-userChatMessagesOnUpdate in region us-central1
  • Error Failed to create function db-roleGrantsOnWrite in region us-central1
  • Error Failed to create function https-apiCompaniesOnRequest in region us-central1
  • Error Failed to create function https-usersSyncOnCall in region us-central1
  • Error Failed to create function https-adminOnCall in region us-central1
  • Error Failed to create function db-publicChatsOnCreate in region us-central1
  • Error Failed to create function db-groupChatsMembersOnCreate in region us-central1
  • Error Failed to create function db-rolesOnWrite in region us-central1
  • Error Failed to create function db-usersCountOnDelete in region us-central1
  • Error Failed to create function db-usersOnWrite in region us-central1
  • Error Failed to create function https-apiUsersOnRequest in region us-central1
  • Error Failed to create function auth-onDelete in region us-central1
  • Error Failed to create function db-triggersOnCreate in region us-central1
  • Error Failed to create function db-groupChatsMembersOnDelete in region us-central1
  • Error Failed to create function https-messagesOnCall in region us-central1
  • Error Failed to create function db-groupChatsNameOnUpdate in region us-central1
  • Error Failed to create function db-userRolesOnWrite in region us-central1
  • Error Failed to create function db-adminsOnWrite in region europe-west1
  • Error Failed to create function db-userChatMessagesOnCreate in region europe-west1
  • Error Failed to create function storage-onFinalize in region europe-west1
  • Error Failed to create function db-groupChatMessagesOnCreate in region europe-west1
  • Error Failed to create function db-userGrantsOnWrite in region europe-west1

Molnfront avatar Mar 25 '22 11:03 Molnfront

Region is hard coded in some of the functions..

Molnfront avatar Mar 25 '22 12:03 Molnfront

Are those functions tested?

Molnfront avatar Mar 25 '22 12:03 Molnfront

Fixed the second warning in onUpdate.f.js:

Changed map(() => {}) to map(() => ())

With () - implicit return takes place.

Molnfront avatar Mar 25 '22 12:03 Molnfront

Hm, warnings and the failed to create does not seem to be related. for example auth-onCreate has no warnings but fails to deploy.

Molnfront avatar Mar 25 '22 12:03 Molnfront

I have 45 of these in my firebase-debug.log: 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:

Checked the logs at: console.cloud.google.com/logs

severity: "ERROR" textPayload: "Function cannot be initialized. 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 " timestamp: "2022-03-25T11:52:20.862903625Z"

I could not find the termination reason in the vast amount of errors.

Molnfront avatar Mar 25 '22 12:03 Molnfront

The issue must be with the hard coded regions. You can not change a region for a project after it's created. So I am trying to change region to where my project is deployed.

Molnfront avatar Mar 25 '22 13:03 Molnfront

Hm, it still deploys in us-central even when I have changed the hardcoded values with my own multi eu3 region.

? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. Yes i functions: updating Node.js 16 function auth-onCreate(us-central1)... i functions: updating Node.js 16 function auth-onDelete(us-central1)... i functions: updating Node.js 16 function db-groupChatsMembersOnCreate(us-central1)... i functions: updating Node.js 16 function db-groupChatsMembersOnDelete(us-central1)... i functions: updating Node.js 16 function db-groupChatsNameOnUpdate(us-central1)... i functions: updating Node.js 16 function db-publicChatsOnCreate(us-central1)... i functions: updating Node.js 16 function db-roleGrantsOnWrite(us-central1)... i functions: updating Node.js 16 function db-rolesOnWrite(us-central1)... i functions: updating Node.js 16 function db-triggersOnCreate(us-central1)... i functions: updating Node.js 16 function db-userChatMessagesOnUpdate(us-central1)... i functions: updating Node.js 16 function db-userRolesOnWrite(us-central1)... i functions: updating Node.js 16 function db-usersOnWrite(us-central1)... i functions: updating Node.js 16 function db-usersCountOnDelete(us-central1)... i functions: updating Node.js 16 function https-adminOnCall(us-central1)... i functions: updating Node.js 16 function https-apiCompaniesOnRequest(us-central1)... i functions: updating Node.js 16 function https-apiUsersOnRequest(us-central1)... i functions: updating Node.js 16 function https-messagesOnCall(us-central1)... i functions: updating Node.js 16 function https-usersSyncOnCall(us-central1)... i functions: creating Node.js 16 function db-adminsOnWrite(eur3)... i functions: creating Node.js 16 function db-groupChatMessagesOnCreate(eur3)... i functions: creating Node.js 16 function db-userChatMessagesOnCreate(eur3)... i functions: creating Node.js 16 function db-userGrantsOnWrite(eur3)... i functions: creating Node.js 16 function storage-onFinalize(eur3)... i functions: deleting Node.js 16 function db-adminsOnWrite(europe-west1)... i functions: deleting Node.js 16 function db-groupChatMessagesOnCreate(europe-west1)... i functions: deleting Node.js 16 function db-userChatMessagesOnCreate(europe-west1)... i functions: deleting Node.js 16 function db-userGrantsOnWrite(europe-west1)... i functions: deleting Node.js 16 function storage-onFinalize(europe-west1)... ⚠ functions: failed to create function projects/urk/locations/eur3/functions/db-adminsOnWrite ✔ functions[db-adminsOnWrite(europe-west1)] Successful delete operation. ✔ functions[storage-onFinalize(europe-west1)] Successful delete operation. ✔ functions[db-userGrantsOnWrite(europe-west1)] Successful delete operation. ✔ functions[db-groupChatMessagesOnCreate(europe-west1)] Successful delete operation. ✔ functions[db-userChatMessagesOnCreate(europe-west1)] Successful delete operation.

Molnfront avatar Mar 25 '22 13:03 Molnfront

Are the database, strage, hostin and auth services enabled in your project?

TarikHuber avatar Mar 25 '22 13:03 TarikHuber

yes, only. functions that i haven't tried before. But they are getting deployd to the wrong regions

Molnfront avatar Mar 25 '22 14:03 Molnfront

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 i functions: preparing firebase/functions directory for uploading... i functions: packaged firebase/functions (53.38 KB) for uploading ✔ functions: firebase/functions folder uploaded successfully

Molnfront avatar Mar 25 '22 14:03 Molnfront

functions: failed to create function projects/test/locations/eur3/functions/db-adminsOnWrite

Molnfront avatar Mar 25 '22 14:03 Molnfront

and the region that the project is set to seems not working

Molnfront avatar Mar 25 '22 14:03 Molnfront

regions seems a bit random?

Molnfront avatar Mar 25 '22 14:03 Molnfront

is there more hidden regions in the code? this is a real mess.. :-)

Why are the functions by default deployd to us-central1 and europe-west1?

Molnfront avatar Mar 25 '22 14:03 Molnfront

found some edge cases:

Here's a mapping of the closest functions-supported regions for Cloud Firestore and Cloud Storage, for cases where the same region is not supported:

eur3 or europe-west (multi-region) europe-west1

Important: If you are using HTTP functions to serve dynamic content for Firebase Hosting, you must use us-central1.

Molnfront avatar Mar 25 '22 16:03 Molnfront

Ok, I will try to add this to all the functions:

exports.myStorageFunction = functions .region('europe-west1') .storage .object() .onFinalize((object) => { // ... });

Molnfront avatar Mar 25 '22 16:03 Molnfront

You can also remove all regions.

TarikHuber avatar Mar 25 '22 16:03 TarikHuber

Tried a complete deploy.

✖ 10 problems (0 errors, 10 warnings)

✔ functions: Finished running predeploy script. i database: checking rules syntax... ✔ database: rules syntax for database tbvpoc-default-rtdb is valid i firebase.storage: checking firebase/storage.rules for compilation errors... ✔ firebase.storage: rules file firebase/storage.rules compiled successfully i firestore: reading indexes from firebase/firestore.indexes.json... i cloud.firestore: checking firebase/firestore.rules for compilation errors... ⚠ [W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]). ⚠ [W] 13:14 - Invalid function name: exists. ⚠ [W] 13:63 - Invalid variable name: request. ⚠ [W] 18:15 - Invalid function name: get. ⚠ [W] 18:66 - Invalid variable name: request. ⚠ [W] 22:14 - Unused function: isGranted. ⚠ [W] 28:14 - Unused function: isOwner. ⚠ [W] 29:14 - Invalid variable name: request. ✔ cloud.firestore: rules file firebase/firestore.rules compiled successfully i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... ✔ functions: required API cloudfunctions.googleapis.com is enabled ✔ functions: required API cloudbuild.googleapis.com is enabled i storage: latest version of firebase/storage.rules already up to date, skipping upload... ✔ firestore: deployed indexes in firebase/firestore.indexes.json successfully i firestore: latest version of firebase/firestore.rules already up to date, skipping upload... i hosting[tbvpoc]: beginning deploy... i hosting[tbvpoc]: found 15 files in public ✔ hosting[tbvpoc]: file upload complete i database: releasing rules... ✔ database: rules for database tbvpoc-default-rtdb released successfully ✔ storage: released rules firebase/storage.rules to firebase.storage ✔ firestore: released rules firebase/firestore.rules to cloud.firestore i functions: cleaning up build files... i hosting[tbvpoc]: finalizing version... ✔ hosting[tbvpoc]: version finalized i hosting[tbvpoc]: releasing new version... ✔ hosting[tbvpoc]: release complete

✔ Deploy complete!

But I can't find any functions in the console..

Molnfront avatar Mar 26 '22 15:03 Molnfront

You can sheck what projects you have in in your firebase.src file. Also update the firebase rules to version 2.

TarikHuber avatar Mar 27 '22 18:03 TarikHuber

Yes, updated firebase rules to v2. Still got invalid functions names:

i cloud.firestore: checking firebase/firestore.rules for compilation errors... ⚠ [W] 14:14 - Invalid function name: exists. ⚠ [W] 14:63 - Invalid variable name: request. ⚠ [W] 19:15 - Invalid function name: get. ⚠ [W] 19:66 - Invalid variable name: request. ⚠ [W] 23:14 - Unused function: isGranted. ⚠ [W] 29:14 - Unused function: isOwner. ⚠ [W] 30:14 - Invalid variable name: request.

Molnfront avatar Mar 28 '22 13:03 Molnfront

Those look like error messages for code inside the function. Did you change any code or is it from the template?

TarikHuber avatar Mar 28 '22 13:03 TarikHuber

I only added rules_version = "2"; to the firestore.rules file.

No changes other than that and yes it's from the template.

Molnfront avatar Mar 28 '22 13:03 Molnfront

✔ functions: Finished running predeploy script. i database: checking rules syntax... ✔ database: rules syntax for database tbvpoc-default-rtdb is valid i firebase.storage: checking firebase/storage.rules for compilation errors... ✔ firebase.storage: rules file firebase/storage.rules compiled successfully i firestore: reading indexes from firebase/firestore.indexes.json... i cloud.firestore: checking firebase/firestore.rules for compilation errors... ⚠ [W] 14:14 - Invalid function name: exists. ⚠ [W] 14:63 - Invalid variable name: request. ⚠ [W] 19:15 - Invalid function name: get. ⚠ [W] 19:66 - Invalid variable name: request. ⚠ [W] 23:14 - Unused function: isGranted. ⚠ [W] 29:14 - Unused function: isOwner. ⚠ [W] 30:14 - Invalid variable name: request. ✔ cloud.firestore: rules file firebase/firestore.rules compiled successfully 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 storage: latest version of firebase/storage.rules already up to date, skipping upload... ✔ firestore: deployed indexes in firebase/firestore.indexes.json successfully i firestore: uploading rules firebase/firestore.rules... i hosting[tbvpoc2]: beginning deploy... i hosting[tbvpoc2]: found 15 files in public ✔ hosting[tbvpoc2]: file upload complete i database: releasing rules... ✔ database: rules for database tbvpoc-default-rtdb released successfully ✔ storage: released rules firebase/storage.rules to firebase.storage ✔ firestore: released rules firebase/firestore.rules to cloud.firestore i functions: cleaning up build files... i hosting[tbvpoc2]: finalizing version... ✔ hosting[tbvpoc2]: version finalized i hosting[tbvpoc2]: releasing new version... ✔ hosting[tbvpoc2]: release complete

✔ Deploy complete!

No functions is deployd..

Molnfront avatar Mar 28 '22 13:03 Molnfront

If helpful, I had the same issue you described RE "Could not detect language" and I also attempted firebase init functions which yielded the same deploy error.

After sniffing the firebase logs via the console looked like I was missing some npm packages.

I also noticed that the init call from before overwrote my package.json (or maybe I had done that manually when in a frenzy, I don't recall) for the functions and was using node 16 instead of node 14.

May not be the same issue you are having but, basically I just:

  • deleted node_modules, and then overwrote the package.json with a fresh version copied from npx create-react-app my-app --template rmw
  • npm install
  • firebase deploy --only functions

and it worked. I had also removed the hard coded regions.

Here is the fresh package.json I used (valid at the time of writing this): https://gist.github.com/cwmat/b6333c5cc485346ca11693b64b439fdf

cwmat avatar Apr 09 '22 22:04 cwmat

@cwmat thx for pointing that out. Yes the only way to get the latest function updates is to copy them from s fresh package. I'm thinking if maybe firebase extensions would be useful but in the end the flexibility is my main goal with this project to do with it after the template what you want.

TarikHuber avatar Aug 12 '22 17:08 TarikHuber