nodejs modules and duplication? If an app uses two modules that require a common module, does node optimize to prevent loading the same code twice?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:17:35

Read up on how Node.js' module loading caches modules. In your example, the 'http' instance will be the same across all your modules.

But be aware that modules are cached based on resolved filename. When requiring a built-in module like 'http' you can be reasonably sure you're getting the same module object across all your code. But 3rd party packages don't necessarily behave this way. For example, if you require 'express' and 'mime', the 'mime' module object you get will, I believe, be different from the one that's used inside of express. The reason being that express ships with it's own set of module files in it's node_modules subdirectory, while you will have installed and loaded your own copy, probably in your your_project/node_modules somewhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!