firebase-functions: functions.config() returns empty JSON object in localhost.

Version info

"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1",

firebase-functions:

Test case

Steps to reproduce

Running firebase functions in localhost, using Typescript, functions.config() returns an empty JSON object:

import * as functions from 'firebase-functions';

export const hi = functions.https.onRequest((req, res) => res.send(functions.config().test));

And when test it cloud function in localhost it returns an empty JSON object.

Were you able to successfully deploy your functions?

Expected behavior

I already set environment variables by the command:

firebase functions:config:set test.value="Test value" test.id="1"

And I asure that variables executing:

firebase functions:config:get

And it shows test variable value.

If I deploy my firebase functions it works, but to can develop my cloud functions I need to run it in localhost,

Actual behavior

functions.get() returns an empty JSON object.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 18
  • Comments: 25 (4 by maintainers)

Most upvoted comments

You need to run firebase functions:config:get > .runtimeconfig.json or manually create a .runtimeconfig.json with config values if you don’t want to use your production config values. Make sure the command is run in the functions folder. See https://firebase.google.com/docs/functions/local-emulator (the documentation isn’t perfectly clear, we’re in the middle of fixing it!)

For me the .runtimeconfig.json created by firebase functions:config:get > .runtimeconfig.json had the Encoding UTF-16 LE. Resaving .runtimeconfig.json with encoding UTF-8 solved the problem for me.

Heads up to others arriving here, and getting an empty config from functions.config() with firebase serve; the .runtimeconfig.json has to be located in the functions dir, and will not adhere to the source-folder you have specified in firebase.json. For example, if this is your firebase.json

...
"functions": {
    "source": "functions/src/",
....

Your .runtimeconfig.json has to be placed in the functions-folder.

I am still having the same issue .runtimeconfig.json has localized inside functions folder

firebase functions:config:set foo.bar="123456"

firebase functions:config:get > .runtimeconfig.json { "foo": { "bar": "123456" } }

Emulators started firebase emulators:start --only "functions,firestore"

When i try use the command

functions.config().foo.bar

I got this error: It looks like you’re trying to access functions.config().foo but there is no value there. You can learn more about setting up config here: https://firebase.google.com/docs/functions/local-emulator

signature undefined ! TypeError: Cannot read property ‘bar’ of undefined

Looks like this is fixed, i.e. if we run firebase functions:config:get > .runtimeconfig.json in the functions folder, and try to use it inside by calling functions.config(), it loads the config . But I don’t see any documentation. @laurenzlong

Come on google, this issue is 2 years old and there is 0 about it in the docs: https://firebase.google.com/docs/functions/local-emulator

For me, creating the json in the project (!) folder and running firebase functions:config:get > .runtimeconfig.json . inside the project (!) folder worked. Why can’t we just use production variables if not specifying runtimeconfig ?

The problem ist here: https://github.com/firebase/firebase-functions/blob/master/src/config.ts#L74

On Windows PWD does not necessarily exist. The thing is, your terminal probably will be fine, because modern terminals provide PWD, but using firebase-functions in your Javascript code will not resolve your .runtimeconfig.json correctly. You have to manually set PWD, by either doing PWD=<path> <command> (e.g. PWD=/Users/... yarn start) or add it to your global PATH configuration (I think it’s PWD=%PATH%).

@judoole that looks like a bug, thanks for the info.

Just ran into the same issue. It looks like it was introduced in firebase-functions @ 3.4.0. I confirmed it is working properly in 3.3.0.

I was also struggling with this in my Yarn workspace project. The functions “project” is one of the packages in the repo, like this:

my-project/
    packages/
        functions/ <--- This is where the Firebase Functions live
            index.js
            package.json
        mobile-app/
        web-app/
    package.json

I expected .runtimeconfig.json to be in my-project/packages/functions, but it had to be in the root folder my-project! Not what I expected, and it is weird that is has to be this way.

I came across this issue as well and I did have .runtimeconfig.json in the functions folder. In my case I was trying to execute a simple test js file using node. The test file was located at firebase-project-dir > functions > src > aFunctionFolder > test.js.

The test.js file looks like:

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

process.env.GCP_PROJECT = 'my-project';

admin.initializeApp(functions.config().firebase);

const main = () => {
  console.log('Config is', functions.config());
};

main();

I was getting an empty object {} all the time. In my case, the issue was that I was executing this from aFunctionFolder.

i.e:

// pwd = firebase-project-dir
cd functions/src/aFunctionFolder
node test.js // This won't work - Empty object

This won’t work neither:

// pwd = firebase-project-dir
node ./functions/src/aFunctionFolder/test.js // This won't work - Empty object

You need to be explicitly located in the functions folder.

// pwd = firebase-project-dir/functions
node ./src/aFunctionFolder/test.js

firebase-functions uses the current working dir as the root to look for the .runtimeconfig.json file (which actually makes sense), so, to sum up, it’s not enough to just locate the .runtimeconfig.json file where it should be (functions folder) but also make sure you’re calling it from the correct directory (functions folder too!) if you’re using firebase-functions programmatically as in the test file describe above.

EDIT: this is actually what is also stated here https://github.com/firebase/firebase-functions/issues/264#issuecomment-770782535

So I’m confused about something here – are you supposed to check the .runtimeconfig.json file into your Git repo? Or is that just used for local emulator testing? Because I would have thought when Cloud Functions execute (especially in my production environment), functions.config() should return an object with the correct environment data from GCP (e.g. projectId.

.runtimeconfig.json is for your local only, doesn’t need to be in source control as well, for server you need to set config values by firebase functions:config:set key=value

I actually have the same issue still with 3.4.0. Is the config file needed to be named .runtimeconfig.json? I’m using the file name with env.json.

Run this comand from within your functions folder “firebase functions:config:get > .runtimeconfig.json” If using Windows PowerShell, replace the above with: “firebase functions:config:get | ac .runtimeconfig.json”

This worked for me.

Trying all of the above still didn’t work for me. I wrote this solution

run this in your terminal

export CLOUD_RUNTIME_CONFIG=`firebase functions:config:get`

and this in the JS file

let config
if( JSON.parse(process.env.FUNCTIONS_EMULATOR) == true ) {
  config = JSON.parse(process.env.CLOUD_RUNTIME_CONFIG)
}else{
  config = functions.config()
}

The documentation is actually right. .runtimeconfig.json needs to sit within your functions folder: firebase functions:config:get > yourFunctionFolder/.runtimeconfig.json or copy it manually.