Firebase-tools: firebase local server can't connect to firebase database?

Created on 23 May 2017  Â·  27Comments  Â·  Source: firebase/firebase-tools

I'm sorry, I didn't found answer to that but searched much time.

I don't know why this simple example works with firebase deploy but not with local server (firebase serve). Should it work normally?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.test = functions.https.onRequest((req, res) => {
  // This part is totally ignored, and whole function got timeout
  admin.database().ref('chat').once('value').then((snapshot) => {
    res.send({"result": 'OK'})
  });
});

What should I do to test function like this? What I'm missing?

Command: /usr/local/Cellar/node/7.7.1/bin/node /usr/local/bin/firebase serve --debug --only function
CLI Version: 3.9.0
Platform: darwin
Node Version: v7.7.1

Most helpful comment

Hi @stmn This is indeed a bug, thanks for reporting! We are working on the fix. In the meanwhile, here's a workaround:

  1. Go to https://cloud.google.com/console/iam-admin/serviceaccounts
  2. 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”.
  3. Save the file somewhere on your computer
  4. Run
    export GOOGLE_APPLICATION_CREDENTIALS=”absolute/path/to/file.json”
  5. Run
    firebase serve --only functions

All 27 comments

Hi @stmn This is indeed a bug, thanks for reporting! We are working on the fix. In the meanwhile, here's a workaround:

  1. Go to https://cloud.google.com/console/iam-admin/serviceaccounts
  2. 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”.
  3. Save the file somewhere on your computer
  4. Run
    export GOOGLE_APPLICATION_CREDENTIALS=”absolute/path/to/file.json”
  5. Run
    firebase serve --only functions

Hi @laurenzlong , thanks for answer. I'm waiting for fix then.
Unfortunately this workaround not working for me or I'm doing sth wrong.
Here are recorded steps: http://take.ms/PHH0W (it's safe, keys has been removed)

Hey @stmn thanks for the recording! It seems like you made a service account under a different project than the one you're running firebase serve with. They have to be the same project, otherwise it doesn't have the right permissions.

You was right, @laurenzlong. It looks like the projects auto-created through Firebase are not displayed on the Recent Projects tab, and I didn't notice that I need to search them up in the All Projects.

Now it's working! Thanks.

Hi @laurenzlong I've gone through the process you suggested but db calls are still hanging. Went through a second time to make sure I didn't miss anything (correct project, role is project > owner, and correct absolute path and export for downloaded service account json) and still no luck. I'll focus on working on something else until the fix is available. Thanks!

Another thing you can try is:
1) Install and initialize gcloud: https://cloud.google.com/sdk/downloads#interactive
2) Run gcloud auth application-default login in any directory
3) Run firebase serve --only functions

@laurenzlong no luck there either. not sure if its helpful, but for what its worth, I have been able to make calls to admin.auth().createUser and admin.auth().updateUser, its just the ref.once('value') that hangs

@laurenzlong I had the same issue. Using gcloud auth fixed it for now. Thanks!

The fix is now in master branch. You can try it out by:
1) git cloning the repo
2) run "npm link" inside the firebase-tools directory, this will globally link any "firebase" commands you run in any directory to the local version of the CLI.

You can run "npm unlink" to switch back to the installed version.

In case its helpful, jst a heads up to anyone encountering this issue, one thing I was running into is I was using this form for initializeApp

var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

When I switched to the following, I was able to access the db successfully

admin.initializeApp(functions.config().firebase)

Closed since fix is in v3.9.1

@laurenzlong This bug arised again in v3.9.2
Using the exact snippet from this issue

[2017-08-05T20:21:29.636Z] CLI Version: 3.9.2 [2017-08-05T20:21:29.636Z] Platform: win32 [2017-08-05T20:21:29.636Z] Node Version: v6.11.2 [2017-08-05T20:21:29.636Z] Time: Sat Aug 05 2017 22:21:29 GMT+0200 (W. Europe Daylight Time)

info: User function triggered, starting execution info: Execution took 60009 ms, finished with status: 'timeout' info: Execution took 60019 ms, finished with status: 'crash' Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)

Same functions works fine when deployed, just not local.

edit: Using the gcloud solution mentioned here fixes it.

@clintjansen Had it worked for you before in v3.9.1? Also, did you switch the account you used for Firebase at any point?

@laurenzlong It worked on 3.91 and before, I haven't switched accounts.
When I upgraded to 3.9.2 it didnt work anymore, even after downgrading to 3.9.1 I remain having this problem.

Thanks for the info, will dig into this.

Hey @clintjansen can you try this branch? https://github.com/firebase/firebase-tools/pull/417. You can install it by running:
npm install -g https://github.com/firebase/firebase-tools.git#mb-default-creds

Hi @laurenzlong of course, but should I first log out (or remove myself) from the gcloud auth service? If so, what's the correct way to do this?

You can run "gcloud auth revoke --all". Thanks!

@laurenzlong I can confirm that the mb-default-creds branch works as expected. Hopefully the associated PR will be merged and released soon, until then I'll use the firebase-tools installed from the github branch.

I can confirm that this is still broken, and the fix is to logout of Google cloud services and log back in. In my case, the broken behavior only showed up after I did a firebase logout and subsequent firebase login. Something about firebase login must be linked to the current Google cloud services auth token.

Hi @mjackson, do you mean log out from Google cloud services from the browser or via firebase logout? Either way this did not work for me. After a call to the firebase database I get a message;

RESPONSE RECEIVED FROM FUNCTION: 500, {"error":{"code":500,"status":"INTERNAL","message":"function execution attempt timed out"}}

This is only happening when the original project was migrated to project-staging and I used the firebase use --add to switch to that project.

@theadameden The problem occurred for me after I used firebase logout and then firebase login.

I am agree with @mjackson I am having the same problem when I have logout and login again, the problem is still there and I do not know the reason or how to fix it

@mjackson @RicardoRB i`ve got the same problem as you. @laurenzlong commented https://github.com/firebase/firebase-tools/issues/344#issuecomment-303848830 solved the problem for me.

On Windows solved with @laurenzlong comment. After exporting key file firebase serve --only hosting,functions works as expected. tx Lauren

Same thing here. Experienced the following error:

{ Error: Missing or insufficient permissions.
    at /.../functions/node_modules/grpc/src/client.js:554:15 code: 7, metadata: Metadata { _internal_repr: {} } }

while trying to write to firestore.

I had previously used firebase logout and firebase login to change between work and personal accounts.

@laurenzlong's comment above has fixed it for now.

Looks like there hasn't been any recent activity here, and Lauren't comment has proven useful.

If there's more issues with this, please open a new bug with the appropriate information! Thanks!

Was this page helpful?
0 / 5 - 0 ratings