24

David in his repo:

https://github.com/davideast/react-ssr-firebase-hosting

has the file with firebase functions index.js in the main root, not in the /functions directory.

However, if I do the same and drop my index.js file to main root, if I do firebase deploy --only functions it says in the console:

i  deploying functions

Error: functions\index.js does not exist, can't deploy Firebase Functions

Q: How is it possible that he made it work? How can I do the same and deploy successfully functions from other dir than /functions?

Thanks

firebase.json

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "ssrapp"
      }
    ]
  },
  "functions": {
     "source": "/"
  }
}
Patrickkx
  • 1,740
  • 7
  • 31
  • 60

2 Answers2

43

The project workspace that you create with the Firebase CLI contains a file called firebase.json that has a stanza for Cloud Functions that looks like this:

"functions": {
  "predeploy": [
    "npm --prefix $RESOURCE_DIR run lint"
  ],
  "source": "functions"
}

That "source" property defines the name of the folder that contains the code that will run on Cloud Functions. You can change that to whatever you want.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    I did the same as David did - `"source": "**",`. However, it still doesn't work. – Patrickkx Feb 21 '18 at 22:43
  • I just copy pasted his `firebase.json` - https://github.com/davideast/react-ssr-firebase-hosting/blob/master/firebase.json – Patrickkx Feb 21 '18 at 22:44
  • That file only shows configuration for "hosting", which is different than "functions". – Doug Stevenson Feb 21 '18 at 22:45
  • I've posted my `firebase.json` file. Even if I have change the path to `/`, it still says in the console that `functions\index.js does not exist` and can't deploy. I think I may be doing something wrong... – Patrickkx Feb 21 '18 at 22:49
  • 2
    Your "functions" stanza doesn't look anything like mine, and it shouldn't contain any rewrites. Rewrites are for hosting. Hosting is a different product than Cloud Functions, so they have different configurations. Your "functions" stanza should have an immediate child property called "source" which is the name of the folder that contains the source you want to deploy. – Doug Stevenson Feb 21 '18 at 22:53
  • Damn. Im just too sleepy. You are of course right. Thank you, as always. – Patrickkx Feb 21 '18 at 22:56
  • 5
    Is this 'source' for functions field documented anywhere? – pwray Apr 30 '18 at 03:32
  • Perfect. I hit this after moving the functions directory and was met with the rather cryptic "Error: An unexpected error has occurred." after throwing a "TypeError: Path must be a string. Received undefined" exception in FunctionsEmulator. – Chris Eveland May 16 '19 at 14:40
  • @pwray Documented right at the end of [this section](https://firebase.google.com/docs/functions/manage-functions#deploy_functions), above "delete". – maganap Nov 29 '21 at 08:46
13

If you want to use your root directory as your functions folder just change your source key to . in your firebase.json.

  "functions": {
    "source": ".",
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ]
  }

This will look for your index.js file in the root directory (remember to move all the other files to your root directory too).

Documentation: https://firebase.google.com/docs/functions/manage-functions#deploy_functions

Alex Sánchez
  • 930
  • 5
  • 10