Functions-samples: Must functions/index.js contain the full script in one file?

Created on 28 Sep 2017  路  3Comments  路  Source: firebase/functions-samples

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.

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 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!

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rodrigosol picture rodrigosol  路  3Comments

Rovel picture Rovel  路  5Comments

beratuslu picture beratuslu  路  4Comments

rodrigoreal picture rodrigoreal  路  5Comments

nhathiwala picture nhathiwala  路  4Comments