2

I'm writing a HTTP server with nodejs and express and I have a db module using in many other modules.

Is there a problem require this module in many many files?

Does too many requires cause memory leak?

Is it a better way like using global var?

my module is something like this :

module.exports = (function() { return { user : ... }; })(); 

Thanks for any help.

1 Answer 1

6

require() method means load and cache Javascript modules

You can use require as many times as required in your app, It won't be cause of memory leak, Reason when you require a module in a file its loaded and cached and when you require same module in another file again it comes from cache(same reference in other words) so Its actually one time required (loaded) and the module that controls loading, compiling, and caching resides in module.js

Further details on MODULE and Tutorial

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

1 Comment

was answer helpful for you?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.