0

I am following a tutorial.

My understading about firebase is firebase deploy takes time to deploy and propagate. Therefore, developer tends to use firebase serve to get a hot reload feature.

I tried to run this simple code with firebase deploy and firebase serve. The firebase deploy works as expected and I can send a GET request using Postman while firebase serve sent an error.

How to reproduce the environment

  1. npm install -g firebase-tools

  2. firebase init, select functions, select javascript

  3. Add the code down below to index.js

  4. firebase serve

  5. use Postman to send a GET request to api/screams

index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

const express = require('express');
const app = express();

app.get('/screams', (req, res) => {
    admin
        .firestore()
        .collection('screams')
        .get()
        .then((data) => {
            let screams = [];
            data.forEach((doc) => {
                screams.push(doc.data());
            });
            return res.json(screams);
        })
        .catch((err) => console.error(err));
});

app.post('/scream', (req, res) => {
    const newScream = {
        body: req.body.body,
        userHandle: req.body.userHandle,
        createdAt: admin.firestore.Timestamp.fromDate(new Date())
    };

    admin
        .firestore()
        .collection('screams')
        .add(newScream)
        .then((doc) => {
            res.json({ message: `document ${doc.id} created successfully` });
        })
        .catch((err) => {
            res.status(500).json({ error: `something went wrong` });
            console.error(err);
        });
});

exports.api = functions.https.onRequest(app);

error

=== Serving from 'C:\Users\jason\Documents\GitHub\functions'...

!  Your requested "node" version "10" doesn't match your global version "12"
i  functions: Watching "C:\Users\jason\Documents\GitHub\functions\functions" for Cloud Functions...
+  functions[api]: http function initialized (http://localhost:5000/functions/us-central1/api).
i  functions: Beginning execution of "api"
!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
!  External network resource requested!
   - URL: "http://169.254.169.254/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
!  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
>  Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:160:19)
>      at processTicksAndRejections (internal/process/task_queues.js:97:5)
>      at async GoogleAuth.getClient (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:502:17)
>      at async GrpcClient._getCredentials (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-gax\build\src\grpc.js:92:24)
>      at async GrpcClient.createStub (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\google-gax\build\src\grpc.js:213:23)
>  Caused by: Error
>      at CollectionReference._get (C:\Users\jason\Documents\GitHub\functions\functions\node_modules\@google-cloud\firestore\build\src\reference.js:1485:23)
>      at CollectionReference.get (C:\Users\jason\Documents\GitHub\functions\functions

edit: current solution is to use firebase emulators:start directly instead of firebase serve

edit2: add package.json as recommended by Doug Stevenson.

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
Jason Rich Darmawan
  • 1,607
  • 3
  • 14
  • 31
  • The error message seems to indicate a conflict in your node version. A quick search for the error messsage leads to this: https://stackoverflow.com/questions/57070483/firebase-serve-only-functions-doesnt-work/57077858#57077858 – Frank van Puffelen Jul 25 '20 at 14:57
  • 1
    @FrankvanPuffelen Actually, it's the access to metadata.google.internal. that's problematic here. These URLs are internal to Google and can only be used in production to access metadata about the local server instance. Something in this code thinks that it's running inside google rather than on a dev machine. – Doug Stevenson Jul 25 '20 at 16:50
  • 1
    Try making sure all your node modules are fully up to date. I suggest posting your package.json here. There might also be a bug in the emulator, and if so, you'd want to post that to GitHub. https://github.com/firebase/firebase-tools – Doug Stevenson Jul 25 '20 at 16:52
  • @DougStevenson I have remove and reinstall the node_modules both globally and locally. I found that `firebase serve` is based on a depreceated code [stackoverflow reference](https://stackoverflow.com/questions/56618725/firebase-serve-only-functions-vs-local-emulator-to-run-cloud-functions-locally/56620511). I will add the package.json to the post. – Jason Rich Darmawan Jul 28 '20 at 04:14
  • You should use the new emulator suite instead. https://firebase.google.com/docs/emulator-suite – Doug Stevenson Jul 28 '20 at 04:55

0 Answers0