Elyas74
Elyas74

Reputation: 548

handle too many require in nodejs

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.

Upvotes: 2

Views: 734

Answers (1)

Zeeshan Hassan Memon
Zeeshan Hassan Memon

Reputation: 8325

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

Upvotes: 6

Related Questions