Antonio Ooi
Antonio Ooi

Reputation: 1836

Accessing local Firebase Environment Variables when running Node test.js locally

I have the following simple test.js:

const functions = require('firebase-functions');
console.log(functions.config().supportgmail.email);

I've done the following before running node test.js:

  1. firebase functions:config:get > .runtimeconfig.json
  2. firebase emulators:start
  3. node test.js

After running node test.js, I get the following error and the environment variable wasn't output to the console:

{"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. 
Initializing firebase-admin will fail"}

What else do I need to do in test.js in order to make the local Firebase environment variables accessible? Note that when I run the entire web app project locally, it has no problem accessing the local environment variables except when I run a JS independently using node test.js -- I just want to make my newly created function that needs to access the environment variables works before incorporate it into my app project. Thanks!

Upvotes: 2

Views: 3117

Answers (1)

Nibrass H
Nibrass H

Reputation: 2497

This error occurs when functions are deployed with Node v10 ( Please let me know if you are not using Node 10 ). It is caused by a bug in firebase-functions. The Node 10 environment does not include GCLOUD_PROJECT anymore. As a workaround, you can add the following line to your code:

process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId

or you can downgrade to Node 8 until this issue is fixed.

Also please try to redeploy the Cloud Function.

Please also have a look into the following GitHub threads regarding this issue 1, 2.

************ UPDATE **************

As per the following message:

Your requested "node" version "10" doesn't match your global version "12", the message is not an error if not it's just letting you know that your package.json declares a target node version of 10, but you are using node 12 on your machine for emulation. To get rid of the message, please install node 10 instead of 12 on your machine. Or vice-versa.

Please make sure which Node.js version are you using in your package.json file in the engines field.

As per Firebase Official Documentation, it is stated that

Firebase SDK for Cloud Functions 2.0.0 and higher allows a selection of Node.js runtime

and as you are using Node.js, your Cloud Functions SDK should be 2.0.0 or higher, so please follow the next steps or have a look into the Official Documentation to make sure you have the correct SDK installed:

  1. Update SDKs to the latest version if you haven't done it, run the following in the functions folder:

npm install firebase-functions@latest --save npm install firebase-admin@latest --save-exact

  1. Also update Firebase CLI to the latest version:

npm install -g firebase-tools

If you want your local emluation of functions using firebase serve to pick up environment variables, you need to follow the next Documentation.

If you're using custom functions configuration variables, run the following command in the functions directory of your project before running firebase serve.

firebase functions:config:get > .runtimeconfig.json

However, if you're using Windows PowerShell, replace the above command with:

firebase functions:config:get | ac .runtimeconfig.json

And lastly the main point which is missing and stated stated in the Cloud Functions Official Documentation is that this configuration is applied automatically when you initialize the Firebase Admin SDK with no arguments. Please add the following:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

let firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
let project = firebaseConfig.projectId;
console.log(project);

I hope it helps.

Upvotes: 2

Related Questions