缓存


【Caching】

模块在第一次加载后会被缓存。这意味着(除其他情况外)每次调用 require('foo') 时,如果它解析到同一个文件,都会返回完全相同的对象。

【Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.】

只要 require.cache 没有被修改,多次调用 require('foo') 不会导致模块代码被多次执行。这是一个重要特性。通过它,可以返回“部分完成”的对象,从而即使在可能引起循环依赖的情况下,也能加载传递依赖。

【Provided require.cache is not modified, multiple calls to require('foo') will not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.】

要让一个模块执行多次代码,可以导出一个函数,然后调用该函数。

【To have a module execute code multiple times, export a function, and call that function.】