Vladimir Venegas Vladimir Venegas - 11 months ago 90
Node.js Question

How to invoke function with arrow formed

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)

Answer Source

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