How to get Firebase Project Name or ID from Cloud Function
14,323
Solution 1
Thank you @Frank. The answer is:
process.env.GCLOUD_PROJECT
.
I'm not sure where the variable process
comes from, but this does work to get the project name.
Solution 2
Firebase admin SDK has access to that information for you.
const admin = require('firebase-admin');
const projectId = admin.instanceId().app.options.projectId
Solution 3
This worked for me. (node 10)
const FIREBASE_CONFIG = process.env.FIREBASE_CONFIG && JSON.parse(process.env.FIREBASE_CONFIG);
const projectId = FIREBASE_CONFIG.projectId;
Solution 4
try this :
const projectId = admin.instanceId().app['options_'].credential.projectId
Solution 5
For cloud functions running on newer versions of Node:
import firestore from "@google-cloud/firestore";
const client = new firestore.v1.FirestoreAdminClient();
const projectId = await client.getProjectId();
Author by
Coder1224
Updated on June 04, 2022Comments
-
Coder1224 almost 2 years
I am using Cloud Functions and want to get the project name from within one of my Javascript server files. I know that value is stored in the .firebaserc, but I don't think that file is available on the server, right? I want to do something like this:
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.getProjectName(); // or getProjectID()
or
functions.getProjectName();
-
okhobb over 6 yearsprocess is a global object in node. See: nodejs.org/api/globals.html#globals_process
-
okhobb over 6 yearsfunctions.config().firebase.projectId is not defined for me. I'm only seeing fields: databaseURL, storageBucket, apiKey, authDomain, and credential on the functions.config().firebase object.
-
Pier about 6 years
functions.config()
returns environment configurations set by the user, which is why you are getting downvoted. -
Crazometer over 5 yearsThis may not exist depending on how admin was initialized
-
Joseph Lust about 5 yearsDoesn't appear to work on the GCF Node 10 runtime. Don't see it when dumping
process.env
-
torno about 5 yearsMight be better to use
process.env.GCP_PROJECT
now instead sinceGCLOUD_PROJECT
has been marked as deprecated here: cloud.google.com/functions/docs/… -
phatmann about 4 yearsNeither GCLOUD_PROJECT nor GCP_PROJECT are available in Node 10 runtime.
-
Ben Winding over 3 years
GCLOUD_PROJECT
seems to work now, it was fixed in thenode10
runtime too github.com/firebase/firebase-functions/issues/437 -
Leblanc Meneses over 2 yearsadmin.instanceId() is deprecated firebase.google.com/docs/reference/admin/node/…
-
GorvGoyl almost 2 yearsworks as of July 2022