functions-samples: unable to split Firebase functions in multiple files
I’m working with firebase functions and arrived to hundreds of functions, and now it is very hard to manage it in single index.js
file as shown in their lots of examples
I tried to split that functions in multiple files like:
--firebase.json
--functions
--node_modules
--index.js
--package.json
--app
--groupFunctions.js
--authFunctions.js
--storageFunctions.js
in this structure i devide my functions in three categories and put in that three files groupFunctions.js
authFunctions.js
storageFunctions.js
and tried to import that files in index.js
but i don’t know why it is not working for me
Here is groupFunctions.js
var functions = require('firebase-functions');
module.exports = function(){
exports.onGroupCreate = functions.database.ref('/groups/{groupId}')
.onWrite(event => {
console.log(`A group is created in database named:${event.params.groupId}.`);
// some logic...
//...
})
}
Here is index.js
file:
var functions = require('firebase-functions');
module.exports = require("./app/groupFunctions")();
my editor not giving any warning in this code but when i deploy this code with firebase deploy --only functions
it does not deploy function (if some functions already exist on firebase console, it remove all functions on deploy)
So I need help regarding this problem, looking forward to listen from you guise.
question is also asked on stackoverflow
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 7
- Comments: 35 (6 by maintainers)
Now you could even automate this a bit and register functions based on their file name. For instance with an index.js like this:
**UPDATE, this works:**
Where your file structure is like:
EDIT: Edited to reflect new name of environment variable.
I’ve created a slightly modified version of
index.js
that uses globbing to search for files ending with.function.js
. This allows me to organise my directory in any manner.EDIT: Edited to reflect new name of environment variable.
Here is my take on it:
You can have an index.js file that will import and list all other Cloud Functions. One trick to improve performance is to use the
process.env.FUNCTION_TARGET
env variable that will have the name of the Function currently being triggered. During deployment that env variable isnull
.Here is an example index.js for 3 functions:
Then for instance, the ./blurOffensiveImages.js would be:
This will ensure all the 3 functions are deployed if doing a
firebase deploy
but, at runtime, only one function will be imported. So you won’t pollute the functions with each other’s imports.EDIT: Edited to reflect new name of environment variable.
Hi, Please help. I want to write a program on our own https server to add two number without use fire base. So please to run on google home .
@oodavid thx for the great example! 😄 Maybe a little modification that could help. This should remove the problem for @andrewspy
If you don’t ignore the
node_modules
and you have a lot of them the functions deployment could time out. It will deploy all functions but we don’t need it to search over thenode_modules
folder.And just for the record if someone gets an error for the
firebase-admin
initialization. The way @nicolasgarnier wrote his functions is the one that should work.The
try catch
does the trick 😉And the camelCase worksout with this little change:
EDIT: Edited to reflect new name of environment variable.
after trying most of these solutions, only this one worked for me
index.js
foo.js
bar.js
I edited this script to allow for multiple functions within each document
Where each file may export multiple functions
And the script goes through each one identified in each file
The URL’s on firebase are predictably named
Following @boon4376 previous comment, I prefer multiple functions in each file. I have added some “common” files like the following to avoid the SDK being initialized multiple times:
admin.js
The functions are implemented as follows.
realtimedb.js
And the main index.js file is just excluding some particular files.
index.js
EDIT: Edited to reflect new name of environment variable.
I’ve been researching about scalable and convenient firebase functions project structure, and came up with this thread. My implementation below allows for standard firebase function naming convention for objects that will follow your folder structure.
This will allow you to have a folder structure like:
and an export structure like:
One thing I didn’t like with the original suggestion by @armenr is that it collapsed the function name into a single string and bypassed an entire feature that allows structuring the functions based on objects exported.
This not only a styling issue making function names really long and basically hard to use, but also a functional issue as it disables your ability to deploy a group of functions using something like
firebase deploy --only function:users.admin
EDIT: Edited to reflect new name of environment variable.
@sonovice you could add a library like camelCase and do something like this to generate your function names, ie:
this would set the name of
functions/path/to/my/logic.function.js
topathToMyLogic
.(untested logic)
@malikasinger1 I had a similar issue so I had to make a higher order function that takes my firebase objects and returns the cloud function I would like to deploy.
cloudFunction.js
index.js
Hey everyone! After doing a bit of research and finding this thread among others, I’ve decided to release this solution as a package,
better-firebase-functions
, which takes into account the majority of use-cases outlined by everyone here.Your index.js file needs only 2 lines
https://www.npmjs.com/package/better-firebase-functions https://github.com/gramstr/better-firebase-functions
This is my first open-source repo, so let me know what you guys think. Any pull requests welcome!
Well this trick is specifically made when using different Cloud Functions. In your case everything is in the same cloud function but you can still “lazy-load” dependencies the normal way using
require()
if you want. Like this for instance:And you’d have the two files “./users_get.js” and “./users_post.js” doing whatever is only needed for one specific handler.
This optimisation is not that useful though because eventually both handlers run on the same Cloud functions instance so they will get both called on the same instance after a while. But still you could do it, it would help a little bit spread the static init over time.
In the case of entirely different Cloud Functions we do this optimisation because we’re entirely sure that the code will never be ran on the same instance so there is really no point in doing all the static initialisation for other functions.
The
FUNCTION_TARGET
trick could be written in a more concise way:EDIT: Edited to reflect new name of environment variable.
This thread is great. Props to @TarikHuber for his Medium post that brought me here.
My working implementation for single function/file is as follows:
EDIT: Edited to reflect new name of environment variable.
@andrewspy with the
index.js
file I suggested, I’m globbing for files that match'./**/*.function.js'
, sohelloWorld.js
won’t get matched. Rename it tohelloWorld.function.js
and see if that changes anything.If not, I’ll make an example repo.