9

I am building firebase function with javascript. Now i have a lot of inter-call function and i plan to move those function into different file to avoid index.js become very messy.

So below is the current file structure:

/functions
   |--index.js
   |--internalFunctions.js
   |--package.json
   |--package-lock.json
   |--.eslintrc.json

I want to know:

1) How to export the function from internalFunctions.js and import it to index.js.

2) How to call internalFunctions.js function from index.js.

My code is written in JavaScript.

Edited

internalFunction.js will have multiple functions.

Jerry
  • 1,455
  • 1
  • 18
  • 39
  • Already answered here: https://stackoverflow.com/questions/43486278/how-do-i-structure-cloud-functions-for-firebase-to-deploy-multiple-functions-fro – AarónBC. May 09 '18 at 02:27
  • Which one is the appropriate solution? because the accepted answer's comment actually telling my concern, which i don want to export the internalFunctions.js functions again in index.js. I want to call a function in internalFunctions.js from index.js only. – Jerry May 09 '18 at 02:40
  • Sorry, I should be more explicit, I added an answer, you can see that the importing is the same as the post except that you need to use it differently. – AarónBC. May 09 '18 at 03:06

1 Answers1

17

First you set the function in your file:

internalFunctions.js:

module.exports = {
    HelloWorld: function test(event) {
        console.log('hello world!');
    }
};

Or if you dont like a lot messing with curly braces:

module.exports.HelloWorld = function(event) {
    console.log('hello world!');
}

module.exports.AnotherFunction = function(event) {
    console.log('hello from another!');
}

There are also other styles you can use: https://gist.github.com/kimmobrunfeldt/10848413

Then in your index.js file import the file as a module:

const ifunctions = require('./internalFunctions');

And then you can call it directly within your triggers or HTTP handlers:

ifunctions.HelloWorld();

Example:

//Code to load modules 
//...
const ifunctions = require('./internalFunctions');

exports.myTrigger = functions.database.ref('/myNode/{id}')
    .onWrite((change, context) => {

      //Some of your code...        

      ifunctions.HelloWorld();

      //A bit more of code...

});
AarónBC.
  • 1,280
  • 9
  • 14
  • I have additional question, how to call a function at internalFunctions from internalFunctions? (within the same file) – Jerry May 09 '18 at 14:30
  • Use `module.exports.HelloWorld();` or just `exports.HelloWorld();` – AarónBC. May 09 '18 at 15:51
  • I mean like, inside HelloWorld() function, i want to call AnotherFunction() to process something, how do i call it? – Jerry May 09 '18 at 18:24
  • Yes, within a function in internalFunctions.js you can use module.exports.HelloWorld(); to call a local function. – AarónBC. May 09 '18 at 20:21
  • If you don't like it you can use another style of declaring and exporting your functions like the ones provided in the link of the answer: https://gist.github.com/kimmobrunfeldt/10848413 – AarónBC. May 09 '18 at 20:22
  • Are we still able to pass input to these functions from internalFunctions.js? – Jonathan Dec 14 '18 at 20:31