In order to organize my project, I have decided to spread my cloud functions into multiple files, following this documentation : https://firebase.google.com/docs/functions/organize-functions?gen=1st#write_functions_in_multiple_files
It works good, but now I would like to have shared internal functions in order to avoid code duplication.
Here is what I tried:
I have created a sharedFunctions.js file at the root level containing some test functions like:
function sayHello1(){
console.log('Hello world1');
}
function sayHello2(){
console.log('Hello world2');
}
And a testSharedFunction.js file containing:
const functions = require('firebase-functions');
const sharedFunc = require('./sharedFunctions');
// firebase deploy --only functions:testSharedFunction
exports.testSharedFunction = functions.https.onRequest((request, response) => {
sharedFunc.sayHello1();
return true;
});
But when I tried to deploy my testSharedFunction function for testing it, I get this error: Error: Error parsing triggers: Cannot find module './sharedFunctions
Do you have and idea of what could be wrong , or how I can succeed to achieved having those internal shared functions ?
Thank you for your help, Benjamin
You are not exporting the
sayHello1()andsayHello2()functions from thesharedFunctions.jswhich is why you are getting this error. Updated code will be :sharedFunctions.js :
And importing those in main file as follows :
testSharedFunction.js :
You were using
CommonJS-style module exports in Node.js.but you should give a try toES6 modules importswhich will simplify as following : sharedFunctions.js :And importing will become :
testSharedFunction.js :