0

I have a fairly large function in my module (./myMainModule.js) that I would like to be packaged in a separate module (./mySubModule. I'm trying to figure out how to require(mySubModule) and then have a function from mySubModule be used as a function in myMainModule.

I've tried to export

mainFunction: mySubModule.subFunction(params),

but it's saying the params are not defined.

//myMainModule.js
const mySubModule = require("./mySubModule)

module.exports = {
   mainFunction: mySubModule.subFunction(params),
}



//----------

//mySubModule.js

module.exports {
  subFunction: function(params) {
     console.log(params);
  }
}

I'm getting params is not defined when assigning the function in myMainModule.

3
  • 1
    An = is missing in mySubmodule... it should be module.exports = { Commented Jul 23, 2019 at 20:37
  • Don't call the function (with what params value?) when you want to export the function itself, not the result of a call. Commented Jul 23, 2019 at 21:43
  • I see where I went wrong now, thank you! Commented Jul 24, 2019 at 0:29

1 Answer 1

1
const subModule = require("./subModule");

module.exports = {
     mainFunction: subModule.subFunction,
     // This works ^^

     //mainFunction : subModule.subFunction(params),
     // Not this ^^

} 

My linter was complaining that my params were not defined. I was actually calling the function when I should have been just referencing it. I did not need to worry about the parameters.

I was actually calling the function and that's why my linter was saying my params were not defined.

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

2 Comments

Welcome to Stackoverflow. Would you mind extending your answer for fellow programmers; who might understand how it helps to solve the problem.
This answer has been flagged as potentially low-quality. Please elaborate on your answer and why you think it solves the OP's question so that new and future developers understand it better. - From Review

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.