Sorry if I am asking an obvious question but I am new to the Nodejs world.
If I have a complex backend cloud function script which I want to break into separate files, what is the proper method to do so?
I tried placing the functions into their own files, and in the index.js I "require (FILENAME)' and call the functions inside modules.export. Failed.
I read through all the samples in this repo but there is not a single one that has more than one index.js for the backend. Please show me how a multi-file script should be designed.
EDIT: I see the same question has been asked on StackOverflow https://stackoverflow.com/questions/42958719/organize-cloud-functions-for-firebase and the implied answer is that modules can be used, but since I am new to the Javascript world and is confused by its constant evolution, I hope you can write a simple guide showing the exact syntax for such a multi-file backend so we can just copy and paste.
This is an example using modules: https://github.com/firebase/functions-samples/blob/master/user-data-cleanup/functions/index.js#L22
This is how you export functions in the required file: https://github.com/firebase/functions-samples/blob/master/user-data-cleanup/functions/wipeout.js#L43-L45
Basically you need to require the file that you want to import/use. In that file you need to export functions (only exported functions/values/classes) are usable through require the rest is kinda "private" to that module.
Basically all you need to do if you have index.js and mylogic.js is:
in index.js
const mylogic = require('./mylogic'); // import the file
mylogic.myfunction(); // use the exported function
in mylogic.js
// Export the myfunction function.
exports.myfunction = function(){
// my function code.
}
That's one way There are other way such as defining the exports object in the end which some people prefer and there is the new import/export syntax coming up in next version of Node (not yet supported natively in Cloud Functions)
Cheers!
Thanks! I also managed to resolve by reading more beginner-friendly JS guides, like this one http://adrianmejia.com/blog/2016/08/12/Getting-started-with-Node-js-modules-require-exports-imports-npm-and-beyond/ I guess my Javascript-fu was too low that I had to trip over some very fundamental language issues.
But then I was forced to use Nodejs because of Firebase cloud functions, and most of my Python/Ruby/PHP seems to be of no help here.
On a similar note, I lost a few days testing my code, only to realise that cloud function names cannot have underscore in them, like "venues_with_keys" fail deploy, but "venueswithkeys" passed. And there was no warning about this in the documentation.
This is an example using modules: https://github.com/firebase/functions-samples/blob/master/user-data-cleanup/functions/index.js#L22
This is how you export functions in the required file: https://github.com/firebase/functions-samples/blob/master/user-data-cleanup/functions/wipeout.js#L43-L45
FYI, these files have now moved to their own repo:
https://github.com/firebase/user-data-protection/blob/master/functions/index.js#L22
https://github.com/firebase/user-data-protection/blob/master/functions/wipeout.js#L43-L45
Most helpful comment
This is an example using modules: https://github.com/firebase/functions-samples/blob/master/user-data-cleanup/functions/index.js#L22
This is how you export functions in the required file: https://github.com/firebase/functions-samples/blob/master/user-data-cleanup/functions/wipeout.js#L43-L45
Basically you need to
requirethe file that you want to import/use. In that file you need toexportfunctions (only exported functions/values/classes) are usable throughrequirethe rest is kinda "private" to that module.Basically all you need to do if you have
index.jsandmylogic.jsis:in index.js
in mylogic.js
That's one way There are other way such as defining the
exportsobject in the end which some people prefer and there is the newimport/exportsyntax coming up in next version of Node (not yet supported natively in Cloud Functions)Cheers!