1

i have written a function in file hello.js


dep1=require('dependency');
function hello(Args, callback){
    modifiedData= dep1.someFunction(Args);
    console.log(modifiedData);
    callback(modifiedData);
}
module.exports=hello;

how would i re-use this function in other file?

h=require("./hello");
h.hello("Howdy!", function(err,args){
    do something;
}

Any pointers?

1 Answer 1

2

That looks acceptable, although it is a bit hard to read. But when your callback has err as the first argument, make sure you send a null object as the first parameter:

callback(null, modifiedData);

When you are using module.exports, then the module itself can be called as that function. So you will reuse that function like this:

h = require("./hello");
h("Howdy!", function(err, args) {
    //do smth
});

Otherwise, in order for your example to work, just delete module. and add the name (can be a different one):

exports.hello = hello;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Randunel, i was calling hello.hello so it was throwing error.
how to import all functions of that file? for each function i need to write exports.fun1=fun1,exports.fun2=fun2...n time?

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.