Firebase-tools: `firebase serve` fails with TypeError on Travis CI

Created on 5 Jun 2017  Â·  27Comments  Â·  Source: firebase/firebase-tools

Hey there! I'm trying to run firebase serve on Travis CI to run smoke tests. App fails to start with Error: An unexpected error has occurred.:

i  functions: Preparing to emulate HTTPS functions. Support for other event types coming soon.
[2017-06-05T12:44:01.457Z] TypeError: Cannot read property 'refresh_token' of undefined
  • I'm using firebase serve --debug --only functions,hosting --token $FIREBASE_TOKEN --project $FIREBASE_PROJECT to run it
  • I also have sudo: required in Travis config
  • Everything works fine with same setup locally

Versions:

[email protected]
[email protected]
[email protected]

Any ideas?

Most helpful comment

Hi. I get the same error when I tried to run serve functions in a docker image.

Looks like the problem is the empty configstore. The auth does not set the token from the cli parameters because this line never executed:
https://github.com/firebase/firebase-tools/blob/master/lib/auth.js#L230
(the currentRefreshToken === undefined and refreshToken equal with the --token parameter)

So when ensureDefaultCredentials read the tokens from the configstore it return with undefined

All 27 comments

Hey @sergeybekrin thanks for the report! This is definitely an edge case that we overlooked.

In the meanwhile, here's a workaround.

  • Go to https://cloud.google.com/console/iam-admin/serviceaccounts
  • Click “Create service account”, give it a name (e.g. emulator), give it the Project>Owner role. Check “Furnish a new private key”, pick “JSON”.
  • Save the file somewhere
  • Run export GOOGLE_APPLICATION_CREDENTIALS=”absolute/path/to/file.json” as part of the test script, prior to running firebase serve

Please reopen if this doesn't work.

@laurenzlong thanks for support! How can I make sure my exported json file is picked up correctly? As for now I don't see any changes and firebase serve fails with same error.

Can you ensure that you've given the absolute path to the GOOGLE_APPLICATION_CREDENTIALS environment variable? Can you also try printing out process.env.GOOGLE_APPLICATION_CREDENTIALS to ensure that the environment variable has been set properly?

@laurenzlong yep, everything looks good. GOOGLE_APPLICATION_CREDENTIALS path is absolute, file is readable.

Few more things:

  • firebase fails to start before my app, that means functions/index.js is not even initialized
  • I don't see it in debug output to make requests to specified endpoints in service *.json, they are still different (not sure if this is actually a problem)

@laurenzlong I've tried to debug token auth flow logic with no luck. Only thing I can assume is that it trying to read refresh_token from config before setting it. Do you have any other ideas what might be the problem?

Hey @sergeybekrin, are you making any API requests within your functions? As well, can you try again but remove the "--token $FIREBASE_TOKEN" flag? And do you see the same error if you run firebase serve with just hosting and not functions?

@laurenzlong behaviour is changed after I updated deps and used FIREBASE_TOKEN instead of --token, at least I don't see TypeError anymore:

  • I'm using functions.config().firebase and firebase.initializeApp(config), no other direct API calls.
  • firebase serve --only hosting works perfectly fine.
  • firebase serve --only functions,hosting does't not fail anymore, but hangs after âś” hosting: Local server: http://localhost:5000 message — it should run functions after that I believe.
  • firebase serve --only functions fails with Error: No targets found. Valid targets are: functions,hosting only on Travis.

Hey @sergeybekrin the behavior is extremely bizarre and I'm out of ideas. Could you try writing unit tests instead of using firebase serve inside Travis? Here's a guide on how to do it: https://firebase.google.com/docs/functions/unit-testing

Please do let me know if you do end up figuring this out. I'd be very curious about what works.

Sorry I can't be more helpful! You can also try posting a question to Stack Overflow and see if anybody else has figured it out.

Actually I'll leave the issue open in case anybody else wants to jump in.

@laurenzlong thanks, this is definitely pretty weird issue. I'll update this discussion if I came up with something.

Having the same issue trying to use firebase serve --only functions --debug inside of a docker container for local development and testing, the initial HTTP calls for the token do respond with 200s.

functions_1           | i  functions: Preparing to emulate HTTPS functions. Support for other event types coming soon.
functions_1           | [2017-08-18T08:26:51.746Z] TypeError: Cannot read property 'refresh_token' of undefined
functions_1           |     at module.exports (/usr/local/share/.config/yarn/global/node_modules/firebase-tools/lib/ensureDefaultCredentials.js:31:45)
functions_1           |     at Object._startEmulator [as start] (/usr/local/share/.config/yarn/global/node_modules/firebase-tools/lib/serve/functions.js:91:3)
functions_1           |     at /usr/local/share/.config/yarn/global/node_modules/firebase-tools/lib/serve/index.js:16:19
functions_1           |     at arrayMap (/usr/local/share/.config/yarn/global/node_modules/lodash/lodash.js:660:23)
functions_1           |     at Function.map (/usr/local/share/.config/yarn/global/node_modules/lodash/lodash.js:9571:14)
functions_1           |     at _serve (/usr/local/share/.config/yarn/global/node_modules/firebase-tools/lib/serve/index.js:14:21)
functions_1           |     at Command._action (/usr/local/share/.config/yarn/global/node_modules/firebase-tools/commands/serve.js:42:12)
functions_1           |     at /usr/local/share/.config/yarn/global/node_modules/firebase-tools/lib/command.js:176:29
functions_1           |     at tryCatch (/usr/local/share/.config/yarn/global/node_modules/rsvp/dist/rsvp.js:525:12)
functions_1           |     at invokeCallback (/usr/local/share/.config/yarn/global/node_modules/rsvp/dist/rsvp.js:538:13)
functions_1           | 
functions_1           | Error: An unexpected error has occurred.
functions_1           | error Command failed with exit code 2.

This is when using either --token $FIREBASE_TOKEN or the env variable FIREBASE_TOKEN.

I tried v3.9.1 and had the same error, but it does work fine with v3.9.0.

Same problem here.. it works when using serve hosting but fails when serving functions

Hi. I get the same error when I tried to run serve functions in a docker image.

Looks like the problem is the empty configstore. The auth does not set the token from the cli parameters because this line never executed:
https://github.com/firebase/firebase-tools/blob/master/lib/auth.js#L230
(the currentRefreshToken === undefined and refreshToken equal with the --token parameter)

So when ensureDefaultCredentials read the tokens from the configstore it return with undefined

Hey, @laurenzlong!

Any news about this issue? It prevents us from running integration tests for Firebase Functions on CI which is very frustrating!

I would be glad to help with PR, but I can't figure out how to properly pass the token to the auth module. I can't use deploy command as an example as there it uses completely different authentication approach. Could you help me with the direction?

having the same problem :(

Hey everyone, we just released firebase-functions-test, a testing SDK specifically for writing unit tests. That would be better suited for CI systems than using firebase serve. Check it out (repo, doc), and let me know if this doesn't suit your use case for some reason.

@laurenzlong My particular use case is to simply run firebase serve inside a docker container in general, not necessarily in a CI context. As a few others have mentioned in this thread, having a FIREBASE_TOKEN env variable in the container does not work.

yarn run v1.5.1
$ firebase serve --only functions -p 5000 --token <FIREBASE_TOKEN>

=== Serving from '/<DIRECTORY_NAME>'...

i  functions: Preparing to emulate functions.

Error: An unexpected error has occurred.
error An unexpected error occurred: "Command failed.
Exit code: 2
Command: sh
Arguments: -c firebase serve --only functions -p 5000 --token <FIREBASE_TOKEN>
Directory: /<DIRECTORY_NAME>
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/<DIRECTORY_NAME>/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

hosting is working for me.

$ yarn firebase serve --only hosting -p 8000 --token $FIREBASE_TOKEN

  "scripts": {
    // ...
    "firebase": "firebase",
    // ...
  },
$ yarn firebase serve --only functions --port 5000 --token $FIREBASE_TOKEN
yarn run v1.6.0
$ firebase serve --only functions --port 5000 --token $FIREBASE_TOKEN

=== Serving from '/Vue-Fullstack-Starter'...

i  functions: Preparing to emulate functions.
Warning: You're using Node.js v8.11.3 but Google Cloud Functions only supports v6.11.5.
error: /Vue-Fullstack-Starter/node_modules/firebase-admin/lib/auth/credential.js:94
            throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse refresh token file: ' + error);
            ^

Error: Failed to parse refresh token file: Error: Refresh token must contain a "refresh_token" property.

@laurenzlong I am working on an integration test for callable HTTPS function. AFAIK, the firebase-functions-test module is not (yet) compatible with callable https functions. Can you confirm?

For that reason, I too am running the functions emulator inside a dockerized environment in our CI/CD pipeline. Updating to the latest release made it work, but I'm wondering if this is generally a bad idea?

Thanks for your time!

Hey @codyzu, yes support for callable functions in firebase-functions-test is coming, thanks for your patience. Running the emulator in a dockerized environment isn't generally a bad idea. The only trade off is that it's slower than running unit tests written with firebase-functions-test.

Hello, I do not know if this is related but I am unable to get firebase-admin (Node.js) to communicate when inside a CI/CD docker container.

https://stackoverflow.com/questions/52655375/how-do-i-put-firebase-admin-credentials-in-an-environment-variable

Basically no matter what I do, firebase-admin write/read tests work on my dev machine, but when it's in a CI/CD container (tested Codeship and Bitbucket Pipelines) I get this error, which repeats until the Jest runner times out.

[2018-10-05T12:53:51.316Z] @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Failed to parse access token response: Error: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND\"."}

Is this related? https://github.com/kubernetes/kubernetes/issues/8512

@lukepighetti I'm not an expert on firebase-admin, I suggest looking through the issues in that Github repo, and filing a new one if it doesn't exist yet: https://github.com/firebase/firebase-admin-node

Hi Lauren, forgot to follow up. I found the solution to my problem, it was a typo when initializing admin. Thanks for the quick reply!

Thanks for the update!

Was this page helpful?
0 / 5 - 0 ratings