nodejs模块和复制?如果应用程序使用两个需要公共模块的模块,节点是否优化以防止加载相同的代码两次? [英] 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?
问题描述
换句话说,我最终得到一个应用程序:
//主应用程序创建一个包含本地实例的闭包的http,一个proxy1
/ pre>
//的实例和一个proxy2的实例,这两个实例都是从封闭返回的函数,它们具有范围为http的实例
var http = require('http'),
httpProxy1 = require('./ proxy1'),
httpProxy2 = require('./ proxy2');
/ * ...使用http,使用proxy1或proxy2适当的东西... * /
// proxy1创建一个包含本地http的实例,并公开单个公共方法
var http = require('http');
module.exports = function(foo){/ * ...做与http ... * /}的东西
// proxy2创建一个包含http和公开的本地实例的闭包单个公共方法
var http = require('http');
module.exports = function(foo){/ * ...使用与东西无关的东西proxy1做... * /}
如果我还想独立使用proxy1,将其作为模块是有意义的,但即使是一个小项目,也可能导致许多模块所有这些都需要核心模块,特别是http和fs
解决方案了解Node.js的模块加载方式缓存模块。在您的示例中,所有模块中的http实例将是相同的。
但请注意,模块将基于解析的文件名进行缓存。当需要像http这样的内置模块时,您可以确保在所有代码中获得相同的模块对象。但是第三方软件包并不一定这样做。例如,如果你需要'express'和'mime',那么你所得到的'mime'模块对象将与express中使用的对象不同。原因是它在其node_modules子目录中具有自己的一组模块文件,而您将安装并加载自己的副本,可能在您的your_project / node_modules某处
Apologies if this is a dumb question, but if I create 2 modules that both require('http') and my main application that requires both modules, or requires modules that in turn require both modules, while also requiring 'http' for its own purposes, do I end up with three instances of the http module, each locked within the scope of a different closure, or does node rewrite things to avoid this?
In other words, do I end up with an app that has:
// main app creates a closure containing a local instance of http, an instance of proxy1 // and an instance of proxy2, both of which are functions returned from closures that have instances of http in scope var http = require('http'), httpProxy1 = require('./proxy1'), httpProxy2 = require('./proxy2'); /* ... do stuff with http, using proxy1 or proxy2 where appropriate ... */ // proxy1 creates a closure containing a local instance of http and exposes a single public method var http = require('http'); module.exports = function (foo) { /* ... do stuff with http ... */ } // proxy2 creates a closure containing a local instance of http and exposes a single public method var http = require('http'); module.exports = function (foo) { /* ... do stuff with http that has nothing to do with the stuff proxy1 does ... */ }
If I also want to use proxy1 independently, it makes sense to have it as a module, but on even a small project, this could lead to many modules that all require core modules repeatedly, especially http and fs
解决方案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
这篇关于nodejs模块和复制?如果应用程序使用两个需要公共模块的模块,节点是否优化以防止加载相同的代码两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!