0

I'm trying to invoke this function, in /file-name.js

'use strict';
exports.handler = (event, context, callback) => {       
    console.log('INFO: Hello World!!');
};

And this is how i am invoking it from /test/tester.js

var myFunc = require('../file-name.js');
myFunc(event, context, callback);

But i get this error:

TypeError: myFunc is not a function

PS: event, context and callback parameters was defined and are ok. PS2: i can't change file-name.js. PS3: Finally work like this (thanks to @ankit31894):

var myFunc = require('../file-name.js');
myFunc.handler(event, context, callback)
Vladimir Venegas
  • 2,515
  • 4
  • 18
  • 42

1 Answers1

2

It has nothing to do with arrow function. Do

myFunc.handler(event, context, callback);

Because you have exported an object which has a property called handler which in turn is your function.

In order to call function in the way you are calling you will have to export the function in /file-name.js

'use strict';
module.exports = (event, context, callback) => {       
    console.log('INFO: Hello World!!');
};

Read difference between exports and module.exports in nodejs

Community
  • 1
  • 1
bugwheels94
  • 26,775
  • 3
  • 35
  • 57