firebase-tools: Cannot access `.runtimeconfig.json` when run from `firebase serve --only functions`

Version info

> firebase --version
3.17.3
> node --version
v6.11.5
> npm --version
5.6.0
firebase-functions: 0.8.1

Steps to reproduce

Locate functions/.runtimeconfig.json:

{
  "environment": {
    "project_id": "development"
  }
}

Locate functions/index.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const functions = require("firebase-functions");
const util = require("util");
// This is key line:
console.log(`environment: ${util.inspect(functions.config().environment.project_id)}`);

exports.foo = functions.https.onRequest((req, res) => {
    console.log(req);
});
//# sourceMappingURL=index.js.map

Run this:

firebase serve --only functions

Expected behavior

functions.config().environment isn’t undefined and functions.config().environment.project_id should be development. Under firebase v3.16.0, the value was successfully accessed.

Actual behavior

The error occurs:

TypeError: Cannot read property 'project_id' of undefined

console.log(environment: ${util.inspect(functions.config().environment)});

console.log(environment: ${util.inspect(functions.config().environment)}); shows reasonable result even at version firebase v3.17.3.

info: environment: { project_id: 'development' }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 8
  • Comments: 19 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Do you have a non-standard directory structure? Does functions/node_modules/firebase-functions exist?

Alternatively, you can also try setting the CLOUD_RUNTIME_CONFIG environment variable to be the config you need, that’ll also get picked up by the SDK.

On Linux/Mac, that would look like running the following in the terminal:

export CLOUD_RUNTIME_CONFIG='{"sendgrid": {"key":"some string here"}}'

In typescript, put your .runtimeconfig.json in build folder

For those who can’t make .runtimeconfig.json work, if you start functions from unusual location or environment, there’s a problem in firebase-functions that assumes .runtimeconfig.json location to be: ../../../.runtimeconfig.json relative to the package.

The solution would be to use env var CLOUD_RUNTIME_CONFIG that accepts both JSON string and absolute (I guess?) path to the .runtimeconfig.json:

env CLOUD_RUNTIME_CONFIG="$(pwd)/build/development/.runtimeconfig.json" \
    firebase serve --project staging

…or:

env CLOUD_RUNTIME_CONFIG="{\"easycron\":{\"token\":\"xxx\",\"group\":\"yyy\"}}" \
    firebase serve --project staging

Unfortunately, if CLOUD_RUNTIME_CONFIG can’t be processed the command silently ignores it, so play around with the path or make sure you escape JSON string if it doesn’t work for you right away.

For powershell, please use the command: firebase functions:config:get | ac .runtimeconfig.json

We will fix our docs to include this info.

firebase functions:config:get>.runtimeconfig.json at firebase project root did not provide config to the running functions in my case, but the CLOUD_RUNTIME_CONFIG env var did work.

Did you run firebase functions:config:get>.runtimeconfig.json in Windows Powershell by any chance?

I recently ran into this issue and figured out that it’s happening for me only in Powershell. Works fine in Command Prompt.

@rolandjitsu Can you give more info?

  • Can you give me the line in your code that accesses config?
  • Did you run firebase functions:config:get>.runtimeconfig.json inside the functions folder prior to emulating?