Firebase-tools: `firebase serve` doesn't pick up `.runtimeconfig.json`

Created on 25 Mar 2018  Â·  14Comments  Â·  Source: firebase/firebase-tools

Version info

3.17.4, 3.17.7

Platform Information

Windows 10 (10.0,6299)
NodeJS v 8.8.0

Steps to reproduce

PS E:\project\functions> firebase functions:config:get > .runtimeconfig.json
PS E:\project\functions> firebase serve --only functions

=== Serving from 'E:\project'...

i  functions: Preparing to emulate functions.
Warning: You're using Node.js v8.8.0 but Google Cloud Functions only supports v6.11.1.
!  functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
!  functions: Error from emulator. FirebaseError: Error occurred while parsing your function triggers.

TypeError: Cannot read property 'private_key' of undefined
    at Object.<anonymous> (E:\project\functions\lib\functions\src\index.js:5:97)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Module.require (module.js:556:17)
    at require (internal/module.js:11:18)
    at C:\Users\Michael\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:18:11
    at Object.<anonymous> (C:\Users\Michael\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:38:3)

index.ts:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const cert = {
    ...functions.config().cert,
    "private_key": functions.config().cert.private_key.replace(/\\n/g, '\n'),
};

admin.initializeApp({
    ...functions.config().firebase,
    credential: admin.credential.cert(cert),
});

Expected behavior

The locally-served functions would pick up the local .runtimeconfig.json file.

Actual behavior

No local (or remote) config is picked up.

Most helpful comment

I've narrowed down the issue to Windows Powershell.

Running firebase functions:config:get > .runtimeconfig.json in powershell generates a broken json, which when parsed gives Unexpected token � in JSON at position 0.

I've managed to sort it out by running .runtimeconfig.json generation command in Windows command prompt.

Hope this helps.

All 14 comments

Updated the original issue to include that this doesn't work on 3.17.7, either.

PS E:\project\functions> yarn list --pattern firebase*
yarn list v1.5.1
├─ @firebase/[email protected]
├─ @firebase/[email protected]
├─ @firebase/[email protected]
├─ @firebase/[email protected]
├─ @firebase/[email protected]
├─ [email protected]
└─ [email protected]
Done in 0.27s.
PS E:\project\functions> firebase --version
3.17.7

Can you run npm install firebase-functions@latest inside your functions folder and try again?

As listed in my comment above, my functions folder is already on [email protected], which is the latest according to https://github.com/firebase/firebase-functions/releases/tag/v0.9.1

I have a minimum repro for this issue: https://github.com/MLoughry/firebase-functions-serve-repro

Hitting the deployed function at https://us-central1-fir-tools-711-repro.cloudfunctions.net/helloWorld:

Hello, world!

Error while trying to serve locally:

=== Serving from 'E:\VSO\firebase-functions-serve-repro'...

i  functions: Preparing to emulate functions.
Warning: You're using Node.js v8.8.0 but Google Cloud Functions only supports v6.11.1.
!  functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
!  functions: Error from emulator. FirebaseError: Error occurred while parsing your function triggers.

TypeError: Cannot read property 'message' of undefined
    at Object.<anonymous> (E:\VSO\firebase-functions-serve-repro\functions\lib\index.js:6:36)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Module.require (module.js:556:17)
    at require (internal/module.js:11:18)
    at C:\Users\Michael\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:18:11
    at Object.<anonymous> (C:\Users\Michael\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:38:3)

@MLoughry I'm not sure how you created .runtimeconfig.json, but there's weird characters in front of it when you run cat .runtimeconfig.json: (Weird thing is that the question marks are not visible when you open it with a text editor, only when you view it within the terminal)

??{
  "cert": {
    "message": "Hello, world!"
  }
}

You test that this is an issue by launching a node repl:

// inside repl:
json = require('.runtimeconfig.json');
// get error: 
SyntaxError: /path/to/.runtimeconfig.json: Unexpected token � in JSON at position 0

Once I replaced your .runtimeconfig.json with another one that contained the same info, but without weird characters, both json = require('.runtimeconfig.json') within a Node shell worked, and firebase serve worked.

I've narrowed down the issue to Windows Powershell.

Running firebase functions:config:get > .runtimeconfig.json in powershell generates a broken json, which when parsed gives Unexpected token � in JSON at position 0.

I've managed to sort it out by running .runtimeconfig.json generation command in Windows command prompt.

Hope this helps.

@MLoughry Did you find a solution for your issue? getting the same error message

@denisyilmaz You can use @amnrzv's suggestion, or just opening a text editor and manually creating a .runtimeconfig.json by typing it out may also work if you don't have too many variables. Seems like Windows adds weird characters when you simply run firebase functions:config:get > .runtimeconfig.json

I'm on Mac and having same issues as @MLoughry described. I tested the .runtimeconfig.json file if it contains the weird character which it doesn't.

How did you go about testing it? And can you ensure that the file is in the functions folder? (not the project root folder)

hah. ok. sorry. moving .runtimeconfig.json solved the problem…

No worries! Glad you figured it out!

@laurenzlong PowerShell is not outputting incorrect/badly formatted JSON. Instead, by default, PowerShell redirects output as UCS-2 Little Endian encoding. i assume firebase serve expects UTF-8 encoding. there's probably not much that can be done on your end. anyhow firebase functions:config:get | ac .runtimeconfig.json outputs UTF-8 in PowerShell.

I've narrowed down the issue to Windows Powershell.

Running firebase functions:config:get > .runtimeconfig.json in powershell generates a broken json, which when parsed gives Unexpected token � in JSON at position 0.

I've managed to sort it out by running .runtimeconfig.json generation command in Windows command prompt.

Hope this helps.

This solution works to this day. Cheers

Was this page helpful?
0 / 5 - 0 ratings