Sunday, 15 April 2018

How to Avoid Requiring the Same Module Multiple Times?

I have several .js files I export and require as modules, in order to separate functionality. For example, I have two files that use web requests:- checkForUpdates.js- getRemoteData.jsSince both files need web requests, they both use:const request = require('request-promise'); However, since const can only be modified once, this creates an error. I could change const to var, and I assume the first require will be cached and used, but I am not sure this is the case or if this is the appropriate way to use require for multiple modules that require the same module. On the other hand, I could require this library before requiring both modules, but then these modules are not able to be used easily in another project and loses context of scope.How should this be handled?Additionally, if there are detailed codebase management guides, please point me to them!

Submitted April 15, 2018 at 04:45PM by _Kai

No comments:

Post a Comment