简体   繁体   English

在 nodejs 中链接导入和导出

[英]Chaining imports and exports in nodejs

I'm looking to set up a file structure such that I have a 'modules' file which exports a bunch of... modules.我希望建立一个文件结构,这样我就有一个“模块”文件,它导出一堆……模块。

I know that in general, any required module is cached and so not read/executed after its first import, but I'm wondering if this stands true for a chain of imports/exports, where their root import has been exported as a defined constant.我知道一般来说,任何需要的模块都会被缓存,因此在第一次导入后不会被读取/执行,但我想知道这是否适用于导入/导出链,其中它们的根导入已作为定义的常量导出.

eg.例如。

// modules.js
const fs = require('fs');

module.exports = {
  fs
};

------------------

// file1.js
const { fs } = require('modules.js');

------------------

// file2.js
const { fs } = require('modules.js');

Do file1 and file2 receive the same cached copy of fs? file1 和 file2 是否收到相同的 fs 缓存副本? This is important because some of my modules require initialization and I don't want this code being executed multiple times.这很重要,因为我的一些模块需要初始化,我不希望这段代码被多次执行。

PS The whole point of this is so that I can have a single location to require specific modules from. PS 重点是这样我就可以有一个位置来要求特定模块。 If there's a better way to do this, please school me!如果有更好的方法来做到这一点,请教我!

Thanks in advance.提前致谢。

Do file1 and file2 receive the same cached copy of fs ? file1file2收到相同的fs缓存副本? This is important because some of my modules require initialization and I don't want this code being executed multiple times.这很重要,因为我的一些模块需要初始化,我不希望这段代码被多次执行。

Yes, modules are cached.是的,模块被缓存。 So, unless you manually reach into the cache to remove a module from the cache, an already initialized module will just be fetched from the cache on the 2nd, 3rd, 4th, etc... times it is loaded.因此,除非您手动进入缓存以从缓存中删除模块,否则只会在第 2、3、4 次等...加载时从缓存中提取已经初始化的模块。 It will only be initialized once.它只会被初始化一次。 This is a fundamental (and very useful) part of the node.js module design philosophy.这是 node.js 模块设计理念的一个基本(并且非常有用)的部分。

The whole point of this is so that I can have a single location to require specific modules from.这样做的全部意义在于,我可以有一个位置来要求特定模块。 If there's a better way to do this, please school me!如果有更好的方法来做到这一点,请教我!

I would strongly discourage you from doing something like you show in your question.我强烈建议您不要做您在问题中所展示的事情。 If you need the fs module, then just include the fs module directly.如果需要fs模块,则直接包含fs模块即可。 There is no savings for doing it the way you propose and it only obscures the real dependencies and ties modules together in ways that makes them harder to reuse individually.按照您建议的方式进行操作不会节省任何费用,它只会掩盖真正的依赖项并将模块联系在一起,从而使它们更难单独重用。 The whole point of modularity is that you can build small to medium sized chunks of separate, reusable and testable code that aren't interwoven with the rest of your code and clearly spell out their own dependencies.模块化的全部意义在于,您可以构建中小型独立、可重用和可测试的代码块,这些代码不与其余代码交织在一起,并清楚地说明它们自己的依赖关系。 Linking things to some intermediate modules module just obscures all that and creates ties between things when that is not necessary.将事物链接到一些中间modules模块只会掩盖所有这些,并在不必要时在事物之间建立联系。 It also can complicate individual module testing.它还会使单个模块测试复杂化。

Look at what typically happens.看看通常会发生什么。 You start creating the modules module.您开始创建modules模块。 FileA needs four things in it so you put those four things in it. FileA 需要四个东西,所以你把这四个东西放在里面。 FileB needs 3 common ones, but two other ones. FileB 需要 3 个常用的,但需要另外两个。 So, now you have 6 things in the modules module.所以,现在你在modules模块中有 6 个东西。 But, you've now made FileA depend on 2 modules in the modules module that it doesn't really depend on and FileB now depends on 1 module in the modules module that it doesn't really depend on.但是,您现在使 FileA 依赖于它并不真正依赖的modules模块中的 2 个模块,而 FileB 现在依赖于它并不真正依赖的modules模块中的 1 个模块。 Extend this a few more ways and you quickly have a big mess of false dependencies.再扩展一些方法,你很快就会得到一大堆错误的依赖。 To actually reuse a given module in another project, you've now got to pull in a lot more than is really necessary.要在另一个项目中实际重用给定的模块,您现在必须引入比真正需要的更多的东西。 In any sort of larger project this can get messy real quickly.在任何类型的大型项目中,这都会很快变得混乱。

Even if you only ever use the module module in cases where another module needs everything in it, what have you actually gained over just specifying the modules you actually need in the module that needs them?即使您只在另一个模块需要其中所有内容的情况下才使用module模块,但仅在需要它们的模块中指定您实际需要的模块,您实际上获得了什么? You've attempted to save a small amount of typing, but complicated the cleanliness of your dependencies and simplicity of reuse.您已尝试节省少量键入,但使依赖项的清洁度和重用的简单性变得复杂。 IMO, not the correct tradeoff. IMO,不是正确的权衡。

For some reason which seems to happen to many when they first start programming on node.js (but the full reasoning isn't entirely clear to me), many people find they want to avoid manually typing the module dependencies at the start of each module.出于某种原因,当他们第一次开始在 node.js 上编程时似乎会发生这种情况(但完整的推理对我来说并不完全清楚),许多人发现他们希望避免在每个模块的开头手动输入模块依赖项. Perhaps it's the notion that we shouldn't ever be repeating lines of code so the 2nd and 3rd time we go to type similar module dependencies into the start of a new module, we have an urge to encapsulate that in some common code.也许这是我们不应该重复代码行的概念,所以第二次和第三次我们将类似的模块依赖项键入新模块的开头时,我们有一种冲动将其封装在一些通用代码中。 That's generally a good notion to follow, but not in this specific case because of the compromise to modularity and independence and testability.这通常是一个值得遵循的好概念,但在这种特定情况下并非如此,因为要对模块化、独立性和可测试性做出妥协。 I think it's just something to get used to when programming with node.js modules and is the better way to code your modules.我认为这只是在使用 node.js 模块编程时习惯的东西,并且是编写模块的更好方法。 Don't create intermediate aggregation modules that add no real value and just obscure the actual dependencies.不要创建没有增加实际价值而只是掩盖实际依赖项的中间聚合模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM