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

Created on 17 Jun 2018  Â·  18Comments  Â·  Source: firebase/firebase-functions

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.

Most helpful comment

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!)

All 18 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!)

It's happening to me. I have the ".runtimeconfig.json" in the "functions" dir generated with "functions:config:get > .runtimeconfig.json". But when I am in localhost and execute "functions.config()" in my Node project it returns an empty object.

EDITED: In Windows you must exec: firebase functions:config:get | ac .runtimeconfig.json

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.

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

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.

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

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

What do you mean by "and try to use it inside"

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.

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.

Great, thanks! Works for me as well.
My folder structure:
.firebase
firebase.json
//...
functions // <= where my functions are living
src
.runtimeconfig.json

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 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.

I just had the same problem after I upgraded various npm packages.
FWIW, the error was probably caused by old globally installed firebase-tools version.

My fix:

npm remove -g firebase-tools
npm install -g firebase-tools

I'm assuming I have to add .runtimeconfig.json to my gitignore?

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 ?

Why is firebase functions:config:get > .runtimeconfig.json not documented…?

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iouhammi picture iouhammi  Â·  24Comments

peterpeterparker picture peterpeterparker  Â·  53Comments

lookfirst picture lookfirst  Â·  25Comments

NikoMontana picture NikoMontana  Â·  23Comments

malikasinger1 picture malikasinger1  Â·  50Comments