Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

98
Visualizações
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. 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.

Example : primitives_blind.js

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

VelbusLib.js

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

index.js (an App)

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

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

A small schema is often better than a long discuss : enter image description here Can someone explain to me the "State-of-the-art" about this? Thanks :)

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I suspect you're looking for object literal spread syntax

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

export * from './primitives_blind.js';
about 4 years ago · Santiago Trujillo Relatório

0

After much research, it appears that's not implicite to have many submodules (ModA, ModB, ModC) and import a module ModX in an app "APP" and use import ModA, ModB, ModC from it.

However, it's possible to export from ModX, some import from ModA or ModB or ModC :

In ModX.js:

import {actionA1, actionA2} from './ModA'
import ModB from './ModB'
import * from './ModC'
...
export { actionA2, ModB }

Then in app.js:

import ModX from './ModX'
...
let Foo = actionA2(something)
let Bar = ModB.actionB(somethingElse)

Thanks @bergi for his correction

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda