模块缓存注意事项


【Module caching caveats】

模块会根据它们解析后的文件名进行缓存。由于模块可能会根据调用模块的位置(从 node_modules 文件夹加载)解析到不同的文件名,因此不能保证 require('foo') 始终返回完全相同的对象,如果它会解析到不同的文件。

【Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee that require('foo') will always return the exact same object, if it would resolve to different files.】

此外,在不区分大小写的文件系统或操作系统上,不同解析后的文件名可能指向同一个文件,但缓存仍会将它们视为不同的模块,并且会多次重新加载该文件。例如,require('./foo')require('./FOO') 会返回两个不同的对象,无论 ./foo./FOO 是否为同一个文件。

【Additionally, on case-insensitive file systems or operating systems, different resolved filenames can point to the same file, but the cache will still treat them as different modules and will reload the file multiple times. For example, require('./foo') and require('./FOO') return two different objects, irrespective of whether or not ./foo and ./FOO are the same file.】