1

I have some questions about module exporting and their inheritance in node.

I know you can export a module with

module.exports = function User(){
   // Lot of code
}

But, how could you add prototypes functions and get them exported as well? Is this automatic?

For example will this code be available to the exported user?

User.prototype.login = function (password) {
     // Much more code
}

And finally, if I require moduleA and moduleB in the main script, will be able moduleB to use those objects or should I require it as well? What route should be used?

1 Answer 1

4

It's automatic, you don't need to do any magic, but I recommend you do module.exports = User; at the end of the file.

If you need moduleB to include something from moduleA you can either:

  • require moduleA inside moduleB
  • pass moduleB a parameter representing the object / function from module A when requiring it.
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I was able to help. By the way, you can also upvote accepted answers on stackoverflow. Cheers

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.