Firebase Functions Deployment Failure - firebase

I am not able to deploy my app into production. I only have firebase hosting and functions. On firebase serve everything works well.
I obtain the following error:
i deploying functions, hosting
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 (46.72 KB) for uploading
+ functions: functions folder uploaded successfully
i hosting[library-app-d49fe]: beginning deploy...
i hosting[library-app-d49fe]: found 19 files in public
+ hosting[library-app-d49fe]: file upload complete
i functions: updating Node.js 10 function app(us-central1)...
! functions[app(us-central1)]: Deployment error.
Function failed on loading 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
Functions deploy had errors with the following functions:
app
To try redeploying those functions, run:
firebase deploy --only "functions:app"
To continue deploying other features (such as database), run:
firebase deploy --except functions
My firebase.json looks like this
{
"hosting": {
"public": "public",
"rewrites": [{
"source": "**",
"function": "app"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
and my app.js in functions folder has the following export:
exports.app = functions.https.onRequest(app);
I am sorry if I don't provide the necessary details but I am clueless of what the issue might be.

Related

Firebase functions missing config json

I've been away from firebase for about 2 years and just got back trying to fix couple of things in my firebase-functions. There was a plenty to update with my packages, but I got to the point where I am not sure what to do next to deploy the fixed function.
Here is the error that happens after trying to deploy a function in terminal:
lukasstateczny#Lukass-MacBook-Pro admin-pukinn % firebase deploy --only functions:mpiEnrollment
=== Deploying to 'pukinn2'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> tslint --project tsconfig.json
Tried to lint /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/functions/src/index.ts but found no valid, enabled rules for this file type and file path in the resolved configuration.
Running command: npm --prefix "$RESOURCE_DIR" run build
> build
> tsc
✔ functions: Finished running predeploy script.
i functions: preparing codebase default for deployment
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error: Cannot find module '../config/pukinn2-1b14e9a65ea1.json'
Require stack:
- /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/functions/lib/index.js
- /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/node_modules/firebase-functions/lib/runtime/loader.js
- /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/node_modules/firebase-functions/lib/bin/firebase-functions.js
lukasstateczny#Lukass-MacBook-Pro admin-pukinn %
Now the config folder exists, but there is no file with this name. Also I'm not sure about these weird numbers after the app name, but I guess it's probably something like a cashed file. I don't know at this point how to get this file and what values should be in it.
ANSWER: After a few helpful comments I realize I was missing serviceAccount.json file that can be found in Firebase Console -> App Settings -> Service Accounts [Tab]
I thought first firebaseConfig was enough but I was wrong.
Also couple of dependencies were outdated, so I needed to upgrade that as well, but after including the .json file the deployment was smooth as ever.
Service account template in case you are not sure how it should look:
{
"type": "service_account",
"project_id": "yourProjectName",
"private_key_id": "...a89fds980asddfs890dsa...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...ahjk8123hdjak91hjkdsa...\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-0000f#yourProject.iam.gserviceaccount.com",
"client_id": "...1234567...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/000/firebase-adminsdk-0000%40yourproject.iam.gserviceaccount.com"
}
Obviously make sure not to share real values with anyone.
I would try removing your .firebase folder which may contain a very old hosting cache file and retry the deployment if you are able to build the app locally just fine.
Additionally, prior to deploying those functions, I would verify in development that they are working as you intend using your Functions Emulator. In the React client you obviously need to use the connectEmulator function for development to be able to test you functions if you haven't already.
You also may want to go inside your functions folder, remove the build folder, run npm run build and ensure your functions are building just fine.

"Cloud Functions API has not been used in project" when only using hosting

I initialized my project for hosting, using the firebase cli. I also let it automatically create the CI script for GitHub.
The configs look like this:
{
"projects": {
"default": "project-name"
}
}
{
"hosting": {
"public": "dist",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
name: deploy on push
"on":
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- run: yarn && yarn build
- uses: FirebaseExtended/action-hosting-deploy#v0
with:
repoToken: "${{ secrets.GITHUB_TOKEN }}"
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_PROJECT_NAME }}"
channelId: live
projectId: project-name
But once the CI script runs on my actions I get the following error:
[2022-10-06T15:45:17.926Z] <<< [apiv2][body] GET https://cloudfunctions.googleapis.com/v1/projects/project-name/locations/-/functions ***"error":***"code":403,"message":"Cloud Functions API has not been used in project 123123123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.","status":"PERMISSION_DENIED","details":[***"#type":"type.googleapis.com/google.rpc.Help","links":[***"description":"Google developers console API activation","url":"https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123"***]***,***"#type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"SERVICE_DISABLED","domain":"googleapis.com","metadata":***"service":"cloudfunctions.googleapis.com","consumer":"projects/123123123"***]***
[2022-10-06T15:45:17.930Z] [functions] failed to list functions for project-name
[2022-10-06T15:45:17.930Z] [functions] HTTP Error: 403, Cloud Functions API has not been used in project 123123123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
[2022-10-06T15:45:17.931Z] FirebaseError: HTTP Error: 403, Cloud Functions API has not been used in project 123123123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
at responseToError (/home/runner/.npm/_npx/7750544ccf494d8b/node_modules/firebase-tools/lib/responseToError.js:49:12)
at RetryOperation._fn (/home/runner/.npm/_npx/7750544ccf494d8b/node_modules/firebase-tools/lib/apiv2.js:288:77)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Error: Failed to list functions for project-name
The process '/usr/local/bin/npx' failed with exit code 1
Error: The process '/usr/local/bin/npx' failed with exit code 1
The thing is, I only want to use the hosting capabilities of firebase and have no plans of enabling firebase functions, so why does it require me to do so? I have created projects in the past, which did not have this requirement.
So it seems like it is a problem with the newest firebase-tools version. After downgrading from 11.14.0 to 11.13.0 locally, it worked again (which I recognized thanks to this comment).
So to make it run in CI, I just installed firebase-tools#11.13.0 as a dev-dependency directly into the project and CI ran through fine again.
Per the error, your script is calling two GitHub Actions and one of these is calling GET https://cloudfunctions.googleapis.com/v1/projects/{project}/locations/-/functions to list the Functions in the Project. This method requires the (cloudfunctions) service to be enabled.
Either enable the service in the project so that the call succeeds
Or remove the step in the script that makes the call.

Firebase Cloud Functions cannot read nitro generated index.mjs file

EDIT:
I want to deploy a Nuxt3 app to Firebase Hosting. I've set the deployment preset for Nitro to "firebase" NITRO_PRESET=firebase, and the build step works fine. However, when I run firebase deploy, I get an error saying:
There was an error reading .output/server/package.json:
.output/server/index.js does not exist, can't deploy Cloud Functions
I checked the .output/server directory, and saw that Nitro generated a file called index.mjs. I checked to see if Cloud Functions also works with .mjs files, but that doesn't seem to be the case. I looked for possible solutions but couldn't find anything; I tried reinstalling my modules, but no luck. Do I have something misconfigured or broken?
I'm following this tutorial:
https://nitro.unjs.io/deploy/providers/firebase
I'm using WSL2 Ubuntu on Windows 11, and the project folder is inside the linux filesystem.
Here is my package.json:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"#nuxtjs/google-fonts": "^3.0.0-0",
"#types/three": "^0.143.1",
"nuxt": "3.0.0-rc.8"
},
"dependencies": {
"firebase": "^9.9.3",
"sass": "^1.54.5",
"three": "^0.143.0",
"vuex": "^4.0.2",
"sass-loader": "^13.0.2"
}
}
My firebase.json:
{
"functions": {
"source": "./.output/server",
"runtime": "nodejs16",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
},
"hosting": {
"site": "my-site",
"public": ".output/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
}
After the build step:
Σ Total size: 19.6 MB (3.21 MB gzip)
✔ You can deploy this build using npx firebase deploy
After the deployment step:
i deploying functions, hosting
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
i functions: preparing codebase default for deployment
Error: There was an error reading .output/server/package.json:
.output/server/index.js does not exist, can't deploy Cloud Functions
It seems you are using the default node-server preset. To deploy the Nuxt 3 app to Firebase, you must set the NITRO_PRESET to firebase in your environment.
NITRO_PRESET=firebase
npm run build
The CLI should prompt you to run npx firebase deploy once the build is completed.
Then run the command and it will deploy the application to Firebase hosting and functions.
You can read more about this in the documentation.

How to deploy express app on firebase hosting

I have built an express app and the folder structure is below.
Then i created firebase init hosting on a dummy folder and copied the firebase.json and .firebase files
I created index.js file
const functions = require('firebase-functions')
const app = require('./app');
exports.widgets = functions.https.onRequest(app);
firebase.json
{
"hosting": {
"public": "public",
"rewrite":[{
"source": "**",
"function": "widgets"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
Also copied the index.html generated by firebase to public folder
On deployment i am getting index.html
If i delete index.html and run as localhost i am getting below output
How can i get the express app executed (as shown in localhost) instead of index.html on firebase deploy.
Edit 1
I am following link.
When i run firebase serve, i am getting this error
AssertionError [ERR_ASSERTION]: missing path at Module.require (module.js:583:3) at require (internal/module.js:11:18) at InitializeFirebaseAdminStubs (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:231:18) at C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:451:9 at Generator.next () at C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71 at new Promise () at __awaiter (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12) at main (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:421:12) at Object. (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:511:5)
When i tried to run firebase deploy
E:\ogst-server-firebase\functions>firebase deploy
=== Deploying to 'ogst-server-95fcc'...
i deploying functions, hosting
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
Error: An unexpected error has occurred.
The reason for this is missing public folder, and i created manually (which was expected to be created with firebase init functions).
E:\ogst-server-firebase\functions>firebase deploy
=== Deploying to 'ogst-server-95fcc'...
i deploying functions, hosting
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i hosting[ogst-server-95fcc]: beginning deploy...
i hosting[ogst-server-95fcc]: found 0 files in public
+ hosting[ogst-server-95fcc]: file upload complete
i hosting[ogst-server-95fcc]: finalizing version...
+ hosting[ogst-server-95fcc]: version finalized
i hosting[ogst-server-95fcc]: releasing new version...
+ hosting[ogst-server-95fcc]: release complete
+ Deploy complete!
Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/ogst-server-95fcc/overview
Hosting URL: https://ogst-server-95fcc.firebaseapp.com
Now my deployment is successful. But i am getting 404
Answer
In the index.js file (following the above link), i did not change
module.exports = functions.https.onRequest(app); //wrong
to
exports.app = functions.https.onRequest(app); //correct
Thanks to all for the support
Firebase Hosting prefers to serve static content over rewrites that get sent to Cloud Functions. In other words, if a request could be served by any static content, that content will always take precedence over a rewrite to Cloud Functions.
If you want your web site root page to be served by Cloud Functions, this means you should not have an index.html in your public folder, since Firebase Hosting is finding that first.

internal server error when deploying NextJs to firebase

I have deployed my NextJS project (adapted from https://github.com/zeit/next.js/tree/master/examples/with-firebase-hosting) to firebase, but when i browse to the URL i get "Internal Server Error".
Deployment did run through successfully:
i deploying database, functions, hosting
+ database: rules ready to deploy.
i functions: ensuring necessary APIs are enabled...
i runtimeconfig: ensuring necessary APIs are enabled...
+ runtimeconfig: all necessary APIs are enabled
+ functions: all necessary APIs are enabled
i functions: preparing src/functions directory for uploading...
i functions: packaged src/functions (571.83 KB) for uploading
+ functions: src/functions folder uploaded successfully
i hosting: preparing src/public directory for upload...
! Warning: Public directory does not contain index.html
i Progress: [....................................................................................................]
+ hosting: 3 files uploaded successfully
i starting release process (may take several minutes)...
i functions: updating function next...
+ functions[next]: Successful update operation.
+ functions: all functions deployed successfully!
+ Deploy complete!
So i feel perhaps it is something to do with the structure of my project? Or firebase.json config? Could someone have a look and let me know?
Here is my firebase.json which is in the root of my project:
{
"database": {
"rules": "database.rules.json"
},
"functions": {
"source": "src/functions"
},
"hosting": {
"public": "src/public",
"rewrites": [
{
"source": "**/**",
"function": "next"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}

Resources