Deploying cloud functions on cli, "Firebase config variables are not available." - firebase

Trying to configure environment variables with firebase sdk. It contains a token which will be used in a google cloud function.
I've followed the docs to a tee and I get this error when I try to redeploy the cloud function.
I've got these at the top of my index.js file
const functions = require("firebase-functions");
const token = functions.config().slack.token;
This is my package.json file
{
"name": "quincygeorge",
"version": "1.0.0",
"description": "smart-office-assistant",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Jonathan Puc",
"license": "ISC",
"dependencies": {
"firebase-admin": "^5.5.1",
"firebase-functions": "^0.7.3",
"slack": "^9.1.0"
}
}
As stated in the docs, I ran "firebase deploy --only functions" first before trying to run...
"gcloud beta functions deploy myfunction --stage-bucket mybucket --trigger-http"
again. But I still get this error.
My firebase tools and all the modules are up to date as I only installed them today.

The error message is complaining about your version of the Firebase CLI.
Update to the latest version. 3.16.0 at the time of this writing.
npm update -g firebase-tools

Related

firebase functions deploy error : ERR_PACKAGE_PATH_NOT_EXPORTED

I was following the in-app-purchase part in Google Codelab, But after I finish all steps and finally run firebase deploy this error has appeared.
I have read many solutions about the simillar problems, for example, remove node-modules and package-lock file then re-install them, down grade node version to 14, downgrade firebase-admin, firebase-functions...
But none of them works for me.
I'm totally desperate now. Please somebody help me. I already googled almost 72hours and I found nothing.
And here is the contents of terminal show up after I run firebase deploy.
% firebase deploy
=== Deploying to 'server-259035'...
i deploying firestore, functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions# lint /Users/functions
> eslint --ext .js,.ts .
Running command: npm --prefix "$RESOURCE_DIR" run build
> functions# build /Users/functions
> tsc
✔ functions: Finished running predeploy script.
i firestore: reading indexes from firestore.indexes.json...
i cloud.firestore: checking firestore.rules for compilation errors...
✔ cloud.firestore: rules file 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 functions: preparing codebase default for deployment
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/firestore' is not defined by "exports" in /Users/functions/node_modules/firebase-admin/package.json
And package.json file.
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"watch": "tsc --watch",
"serve": "firebase emulators:start",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "lib/index.js",
"dependencies": {
"camelcase-keys": "^8.0.0",
"firebase-admin": "^11.0.1",
"firebase-functions": "^3.22.0",
"google-auth-library": "^8.0.2",
"googleapis": "^105.0.0",
"jest": "^28.1.3",
"lodash": "^4.17.21",
"node-apple-receipt-verify": "^1.12.1"
},
"devDependencies": {
"#types/node-apple-receipt-verify": "^1.7.1",
"#typescript-eslint/eslint-plugin": "^5.27.0",
"#typescript-eslint/parser": "^5.27.0",
"eslint": "^8.16.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.26.0",
"firebase-functions-test": "^2.3.0",
"typescript": "^4.7.2"
},
"private": true
}
I had the same issue when updated my package.json packages. I had to make the following change in order to fix it:
Before:
import firestore from "firebase-admin/lib/firestore";
After:
import firebaseAdmin from "firebase-admin";
** Add the prefix firebaseAdmin. to all the firestore references, eg: firebaseAdmin.firestore.Timestamp;*
I changed the following files:
app-store.purchase-handler.ts
google-play.purchase-handler.ts
iap.repository.ts
It happens with in_app_purchases flutter firebase example due to updated package names and their content. Full error is: Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/firestore' is not defined by "exports" in functions/node_modules/firebase-admin/package.json
You need to replace: import firestore from "firebase-admin/lib/firestore"; with import firebaseAdmin from "firebase-admin";
Now you need to go your .ts files and replace the same import above with the new one and correct the references for example: firestore.Timestamp now will be firebaseAdmin.firestore.Timestamp
All the files you need to make changes are these ones:
app-store.purchase-handler.ts
google-play.purchase-handler.ts
iap.repository.ts
I’ve fixed, upgraded and updated codelab example backend functions for verifying in app purcashe on backend. You can find updated code here, it works as expected for me with node 18.
https://github.com/vbalagovic/iap-firebase-backend

Firebase Functions do not build Next.js project when deployed

I'm able to deploy my Next.js project to Firebase Functions successfully as per this post. However, when I try to visit my project in the browser, I get a Error: could not handle the request error, and in the Functions log I see this error message:
Error: Could not find a production build in the '/workspace/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
at Server.readBuildId (/workspace/node_modules/next/dist/next-server/server/next-server.js:151:355)
at new Server (/workspace/node_modules/next/dist/next-server/server/next-server.js:3:120)
at NextServer.createServer (/workspace/node_modules/next/dist/server/next.js:1:2935)
at process._tickCallback (internal/process/next_tick.js:68:7)
I'm not sure how to resolve this though... Adding below my config files:
package.json
{
"main": "server.js", // my next.js app init file
"scripts": {
"dev": "FIREBASE_AUTH_EMULATOR_HOST=localhost:9099 next dev",
"start": "next start",
"build": "next build",
"build:functions": "tsc",
"lint": "npm run lint:next && npm run lint:functions",
"lint:functions": "eslint --ext .js",
"lint:next": "next lint",
"deploy": "firebase deploy --only database,hosting,functions:nextServer",
"emulate": "firebase emulators:start --import=./emulator-data --export-on-exit",
"test": "jest --runInBand --detectOpenHandles",
"cypress": "cypress open"
},
"dependencies": {
// including next
}
}
firebase.json
{
"functions": {
"source": ".",
"runtime": "nodejs10",
"ignore": [
"**/.next/cache/**",
"**/__tests__/**",
"**/components/**",
"**/cypress/**",
"**/emulator-data/**",
"**/functions/**",
"**/layouts/**",
"**/node_modules/**",
"**/pages/**",
"**/public/**",
"**/utils/**",
"**/.*",
"**/*.log",
"**/cypress.json",
"**/database.rules.json",
"**/firebase.json"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build:functions"
]
},
"hosting": {
"target": "my_app",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [{
"source": "**",
"function": "nextServer"
}]
}
}
I assume that, once deployed, Firebase Functions environment would run npm run build and npm run start scripts. Or is the workflow different? Perhaps my scripts are incorrect?
Similar to scripts in package.json files, the predeploy and postdeploy lines are executed before and after a call to firebase deploy when using the Firebase CLI (documented here).
When the Firebase CLI uploads your code to the Cloud Functions staging servers, it is expected to have already been compiled and only needs its runtime dependencies installed. Once an upload has completed, the staging server will execute npm install --production and then prepare your function to handle requests (essentially performing a cold-start, without actually executing the function). If either step fails, you will be shown an error like you have. If both steps succeed, your code with the dependencies installed is persisted as an image and saved to Cloud Storage, ready to use when requests to be handled come in.
In your case, the error seems to be that your .next folder is missing the compiled version of your application.
There are two possible causes of this, either your build prior to deployment is incomplete or when your files are uploaded, something in your ignore list that is required by the deployed code is missing.
For the first part, you modified the predeploy hook in my previous answer from:
{
"functions": {
"source": ".",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
},
...
}
incorrectly to:
{
"functions": {
"source": ".",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build:functions"
]
},
...
}
By changing this, you aren't building your next app prior to deploying the code to the staging server.
In regards to the second part, the deployed next application may be missing some of its dependencies - you can either fine tune the ignore list or you can perform the next build step on the server using a postinstall script in your package.json file:
{
"main": "server.js", // my next.js app init file
"scripts": {
...
"build:next": "next build",
...
"postinstall": "npm run build:next"
},
"dependencies": {
// including next
}
}
Note: If your main file is just JavaScript, you no longer need the typescript compile step defined in npm run build:functions and it can be removed.
I was this error, my solution was:
First, in the packege.json file, in the section scripts, we must modify the property “build”: "next build && next export", and a new property "export": "next export"
Second, we must establish the component and data folders in the root of the project
Third: use the command:
a)You must make log in, so type: firebase login
b)select de option "Configure files for Firebase Hosting and (optionally) set
up GitHub Action deploys"
c)in the question: if you have a build process
for your assets, use your build's output directory?
What do you want to use as your public directory?
You must type: out (really important, it's a folder output)
d) You must type: npm run build
f) last step: type: firebase deploy
package.json
out folder

Firebase deploy issue: Build failed: npm ERR! Maximum call stack size exceeded

I am trying to deploy my Firebase Cloud Function with:
firebase deploy --only functions:helloWorld
My problem is that the command is sometimes successful, but most times it is failing with the error above. What might be the cause, and how to fix it?
I came across a similar question and tried many of the recommended solutions (clearing the npm cache and reinstalling the dependencies). Both solutions do not work reliably for me.
Here is the full console output:
=== Deploying to '<my-firebase-project>'...
i deploying functions
✔ 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 functions directory for uploading...
i functions: packaged functions (84.76 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: current functions in project: myFunction1(us-central1), myFunction2(us-central1), myFunction3(us-central1)...
i functions: uploading functions in project: helloWorld(us-central1)
i functions: updating Node.js 10 function helloWorld(us-central1)...
⚠ functions[helloWorld(us-central1)]: Deployment error.
Build failed: npm ERR! Maximum call stack size exceeded
npm ERR! A complete log of this run can be found in:
npm ERR! /builder/home/.npm/_logs/2020-10-15T17_38_33_530Z-debug.log; Error ID: 49341d49
Functions deploy had errors with the following functions:
helloWorld
To try redeploying those functions, run:
firebase deploy --only "functions:helloWorld"
To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
Part of the problem is that I don't know where to find the full logs that the firebase deploy command output says are in /builder/home/.npm/_logs/2020-10-15T17_38_33_530Z-debug.log
And here are my firebase/functions config files:
./functions/index.js:
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!");
});
./firebase.json:
{
"functions": {
"predeploy": [
],
"source": "functions",
"runtime": "nodejs10"
},
"emulators": {
"functions": {
"port": 5001
},...
}
...
}
./functions/package.json:
{
"name": "functions",
"description": "Backend - Firebase Cloud Functions",
"version": "1.0.0",
"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": "14.3.0"
},
"dependencies": {
"config": "^3.3.1",
"core-util-is": "^1.0.2",
"crypto-js": "^4.0.0",
"firebase-admin": "^9.1.1",
"firebase-functions": "^3.11.0",
"myapp_config": "file:./config"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
Dev stack:
macOS 10.15+
node version = v14.3.0
Turns out the issue was how I was building the local dependency myapp_config in the line
"myapp_config": "file:./config". I re-architected my app to remove [the need for] this line altogether and so far have not seen the deployment error again.
The ./config indicated on that line was actually a symlink to the config folder on my project root that I created with: ln -s ./config ./functions/config originally intended to deliver project-wide configuration-related modules into my functions scripts without having to manually copy over those files into functions. I suppose that much as Firebase Functions supports local Node.js dependencies, the CLI/npm somehow gets confused by or doesn't work well with symlinks (my guess is that it sees ./config and its symlink ./functions/config and goes into some infinite loop between them)
This other question sheds more light on the code structure I had and the problem I was trying to solve in the first place.

Firebase CLI not deploying

I've just installed firebase CLI on to my computer - windows 10..
However, when run npm install in the functions fold I get this warning..
npm WARN dialogflow-fulfillment#0.4.1 requires a peer of actions-on-google#^2.1.3 but none is installed. You must install peer dependencies yourself.
audited 975 packages in 6.407s
30 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Which I do... But then I get this error when I try to deploy the project..
=== Deploying to 'udemy-demo-assistant-59b6a'...
i deploying functions
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
+ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading...
Error: Error parsing triggers: Cannot find module 'actions-on-google'
Require stack:
- C:\Users\Michael\Desktop\Udemy\firebase\functions\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js
- \firebase\functions\index.js
- \AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js
Try running "npm install" in your functions directory before deploying.
and we are back to square one..
How do I resolve this issue
thanks in advance
The package.json
{
"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": "8"
},
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
"dialogflow-fulfillment": "^0.4.1"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}

Firebase deploy error - Cannot find module 'firebase'

I'm trying to deploy Google Cloud function triggers that trigger from db events, but when I run firebase deploy The functions aren't deployed properly and I get an error saying Error: Cannot find module firebase
When I was first creating the project I could deploy functions without any issues. It's been a few days since I last deployed anything, but now I'm getting this issue on all my functions (not just my trigger functions)
My function:
exports.deleteNotificationOnUnlike = functions
.region("us-central1")
.firestore.document("likes/{id}")
.onDelete((snapshot) => {
return db
.doc(`/notifications/${snapshot.id}`)
.delete()
.catch((err) => console.error(err));
});
Package.json
{
"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": "8"
},
"dependencies": {
"busboy": "^0.3.1",
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.1.0"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
},
"private": true
}
Expecting the functions to deploy successfully but getting this error:
Error messages from firebase:
Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'firebase'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/handlers/users.js:5:18)
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)
{"#type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"INVALID_ARGUMENT"},"authenticationInfo":{"principalEmail":"*********"},"requestMetadata":{"requestAttributes":{},"destinationAttributes":{}},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.CreateFunction","resourceName":"projects/*******/locations/us-central1/functions/createNotificationOnComment"}
I've seen some posts where people were missing some modules and needed to run an npm i XXX inside the functions folder, but I haven't seen any instances where it was saying that the firebase module couldn't be found, and I'm not sure what changes from my last successful deployment, or what module I might be missing.
Figured out the solution. I hadn't installed the firebase module in the functions folder but had included it in the parent folder of functions. I went into functions and ran npm install --save firebase and the deploy was successful.
Make sure you installed the package from the functions directory, Not the project directory. I got this error many times when I accidentally installed packages while in the project directory, It worked fine locally, but created such errors during deployment. So, goto functions and install from there
cd functions
npm install [your package]
Then it usually works fine
Try updating firebase npm package, mine was solved using npm install -D firebase.
-Stay Safe!

Resources