Jesper
Jesper

Reputation: 2814

firebase functions:shell can't find custom functions configuration variables

I am trying to test my cloud functions locally. I have a custom functions configuration variable, so I used the command firebase functions:config:get > .runtimeconfig.json to generate a runtimeconfig.json file:

{
  "stripe": {
    "testkey": "someKey"
  }
}

Then I use the command firebase functions:shell to start the emulator, but it prints the following error in my terminal:

+  functions: Emulator started at http://localhost:5001
!  TypeError: Cannot read property 'testkey' of undefined
    at Object.<anonymous> (C:\Users\Jesper\intergun\functions\lib\index.js:14:52)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:532:33
    at Generator.next (<anonymous>)
!  We were unable to load your functions code. (see above)
   - It appears your code is written in Typescript, which must be compiled before emulation.
   - You may be able to run "npm run build" in your functions directory to resolve this.

For some reason it cannot find the variable, and it also seems like it thinks the code is written in TypeScript, even though it is pointing to the generated index.js file. I tried running npm run build but the result is the same.

The top of my index.ts looks like this:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import 'firebase-functions';
import * as Stripe from 'stripe';

admin.initializeApp();
const stripe = new Stripe(functions.config().stripe.testkey);

What am I missing?

Upvotes: 2

Views: 980

Answers (1)

Jesper
Jesper

Reputation: 2814

I found the solution here: https://github.com/firebase/firebase-tools/issues/711

To generate .runtimeconfig.json, I used the command firebase functions:config:get > .runtimeconfig.json. However, this generated a file which seemed to be correct but apparently had invisible characters that shouldn't be there. Instead, I used this command: firebase functions:config:get | ac .runtimeconfig.json. This solved the problem.

Upvotes: 3

Related Questions