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)
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 byfirebase functions:config:get > .runtimeconfig.json
had the EncodingUTF-16 LE
. Resaving.runtimeconfig.json
with encodingUTF-8
solved the problem for me.Heads up to others arriving here, and getting an empty config from
functions.config()
withfirebase serve
; the.runtimeconfig.json
has to be located in thefunctions
dir, and will not adhere to the source-folder you have specified in firebase.json. For example, if this is your firebase.jsonYour
.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
Looks like this is fixed, i.e. if we run
firebase functions:config:get > .runtimeconfig.json
in thefunctions
folder, and try to use it inside by callingfunctions.config()
, it loads the config . But I don’t see any documentation. @laurenzlongCome 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 providePWD
, but usingfirebase-functions
in your Javascript code will not resolve your.runtimeconfig.json
correctly. You have to manually setPWD
, by either doingPWD=<path> <command>
(e.g.PWD=/Users/... yarn start
) or add it to your global PATH configuration (I think it’sPWD=%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 in3.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:
I expected
.runtimeconfig.json
to be inmy-project/packages/functions
, but it had to be in the root foldermy-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 thefunctions
folder. In my case I was trying to execute a simple testjs
file usingnode
. The test file was located atfirebase-project-dir > functions > src > aFunctionFolder > test.js
.The
test.js
file looks like:I was getting an empty object
{}
all the time. In my case, the issue was that I was executing this fromaFunctionFolder
.i.e:
This won’t work neither:
You need to be explicitly located in the
functions
folder.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 usingfirebase-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
.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 withenv.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
and this in the JS file
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.