简体   繁体   English

如何在 NodeJS 中导出模块及其子模块?

[英]Howto export module with it submodules in NodeJS?

I want to create a Velbus (home automation) library to import in any project with Node.JS.我想创建一个 Velbus(家庭自动化)库以使用 Node.JS 导入任何项目。 However, there are plenty of function (sometime 6 or more) on each kind of Velbus modules, so I want to split my actual library into submodules (primitives_blind.js, primitives_relay.js, primitives_tempSensor.js, etc.) I can import them in my main library VelbusLib.js, but I don't know how to allow a user to only import my main module, but use function from submodules.但是,每种 Velbus 模块都有很多 function(有时 6 个或更多),所以我想将我的实际库拆分为子模块(primitives_blind.js、primitives_relay.js、primitives_tempSensor.js 等)我可以导入它们在我的主库 VelbusLib.js 中,但我不知道如何允许用户只导入我的主模块,而是从子模块中使用 function。

Example: primitives_blind.js示例:primitives_blind.js

function blindUP(adr, part) { ... }
function blindDOWN(adr, part) { ... }
function blindStatus(adr, part) { ... }
module.exports = { blindUP, blindDOWN, blindStatus }

VelbusLib.js VelbusLib.js

const blind = require('./primitives_blind.js')

index.js (an App) index.js(一个应用程序)

const velbusLib = require('./VelbusLib.js')
...
velbusLib.blindUP(0x40, 1) // blind.blindUP() not wanted, because user have to import all submodules

Of course, it's not possible to export function imported当然不能导出 function 导入

module.exports = {
    CheckSum,
    Cut,
    // blindUP or blind.blindUP aren't possible

A small schema is often better than a long discuss:一个小的模式通常比一个长的讨论更好: 在此处输入图像描述 Can someone explain to me the "State-of-the-art" about this?有人可以向我解释一下这方面的“最新技术”吗? Thanks:)谢谢:)

I suspect you're looking for object literal spread syntax我怀疑您正在寻找object 文字传播语法

const blind = require('./primitives_blind.js')
module.exports = {
    CheckSum,
    Cut,
    ...blind,
};

which essentially does the same as这基本上与

const blind = require('./primitives_blind.js')
module.exports = {
    CheckSum,
    Cut,
    blindUP: blind.blindUP,
    blindDOWN: blind.blindDOWN,
    blindStatus: blind.blindStatus,
};

However, since you were asking for the state of the art: use ES6 module syntax for the whole library, not CommonJS module.exports , With that it would be但是,由于您要的是艺术的 state:对整个库使用 ES6 模块语法,而不是 CommonJS module.exports ,这样就可以了

export * from './primitives_blind.js';

After much research, it appears that's not possible to have many submodules (ModA, ModB, ModC) and import a module ModX in an app "APP" and use import ModA, ModB, ModC from it.经过大量研究,似乎不可能有很多子模块(ModA、ModB、ModC)并在应用程序“APP”中导入模块 ModX 并从中使用 import ModA、ModB、ModC。

"APP" must import A, B, C too if you need these modules in it.如果您需要这些模块,“APP”也必须导入A,B,C。 "APP" can call components from ModX module and ModX module can call components from ModA, ModB, ModC “APP”可以调用ModX模块的组件,ModX模块可以调用ModA、ModB、ModC的组件

This concept maybe in relation with "tight coupling" pattern...这个概念可能与“紧密耦合”模式有关......

The next question for me is " how to avoid file of code with more than 1000 lines? "我的下一个问题是“如何避免超过 1000 行的代码文件?

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

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