Search code examples
node.jsfirebaseexpressgoogle-cloud-functionsfirebase-cli

Why is Firebase not running my functions folder (for a node app)?


I am trying to host a node app on firebase

The problem is that it keeps timing out when I run a really basic time stamp function.

Here is the firebase.json file:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "rewrites": [{
      "source": "/timestamp",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

Here is the package.json file that is in the functions folder:

{
  "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"
  },
  "main": "index.js",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "jquery": "^3.5.1",
    "node": "^14.8.0",
    "nodemailer": "^6.4.11",
    "nodemailer-mailgun-transport": "^2.0.0",
    "nodemon": "^2.0.4"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Here is the index.js in the funtions folder:

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

const app = express();
app.get('/timestamp', (request, response)=>{
    response.send(`${Date.now()}`);
});

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest(app);

And here is the terminal log when i run firebase locally:

Andrews-iMac:functions test$ firebase serve --only functions,hosting
⚠  Your requested "node" version "10" doesn't match your global version "12"
i  functions: Watching "/Users/test/Desktop/Git/Deep Technology/deep-technology/functions" for Cloud Functions...
i  hosting: Serving hosting files from: public
✔  hosting: Local server: http://localhost:5000
[hosting] Rewriting /timestamp to http://localhost:5001/deep-technology/us-central1/app for local Function app
✔  functions[helloWorld]: http function initialized (http://localhost:5001/deep-technology/us-central1/helloWorld).
i  hosting: 127.0.0.1 - - [18/Aug/2020:17:32:31 +0000] "GET /timestamp HTTP/1.1" 504 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"
[hosting] Rewriting /timestamp to http://localhost:5001/deep-technology/us-central1/app for local Function app
i  hosting: 127.0.0.1 - - [18/Aug/2020:17:33:50 +0000] "GET /timestamp HTTP/1.1" - - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"

Here is a screenshot of me running the local host (you can also see the file structure):

enter image description here


Solution

  • I couldn't figure it out, however, your questions is perfect and I was able to replicate this on my side easily. After few tries, I found it, but it's very small mistake, however such mistakes are hardest to find.

    In presented code function in hosting in firebase.json is different than function exported from index.js. So there are 2 solutions:

    in firebase.json hosting there should be "function": "helloWorld"

    or

    in index.js there should be: exports.app = functions.https.onRequest(app);

    Both are working well on my side!