12

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.

3
  • Already answered here: stackoverflow.com/questions/43486278/… Commented May 9, 2018 at 2: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. Commented May 9, 2018 at 2: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. Commented May 9, 2018 at 3:06

1 Answer 1

19

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

});
Sign up to request clarification or add additional context in comments.

6 Comments

I have additional question, how to call a function at internalFunctions from internalFunctions? (within the same file)
Use module.exports.HelloWorld(); or just exports.HelloWorld();
I mean like, inside HelloWorld() function, i want to call AnotherFunction() to process something, how do i call it?
Yes, within a function in internalFunctions.js you can use module.exports.HelloWorld(); to call a local function.
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: gist.github.com/kimmobrunfeldt/10848413
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.