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

343
Visualizações
How to do ESM dynamic export?

The following exports code used to work when I set the project as CommonJS

let group: keyof typeof cloudFunctions;
// loop through all groups built above and export them
for (group in cloudFunctions) {
  if (group != null) {
    exports[group] = cloudFunctions[group];
  }
}

Now, I am trying to convert this to ESM Module so I use ESM export, instead of CJS exports but it keeps throwing me error.

let group: keyof typeof cloudFunctions;
// loop through all groups built above and export them
for (group in cloudFunctions) {
  if (group != null) {
    export[group] = cloudFunctions[group];
  }
}

The only way I can workaround it is to declare this statically and manually one by one OUTSIDE the for loop at the top-level.

Question: How can I adopt the similar structure I had in CJS in ESM so that I can programmatically export the functions stored inside a variable?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can't do dynamic export with ESM. ESM requires exports to be statically analyzable.

Instead, you can export an object that you define dynamically:

export const someName = {...cloudFunctions};

...or perhaps just export cloudFunctions directly, since it appears to already have what you want to export:

export cloudFunctions;

Code importing from the module will not be able to directly import properties from the object you export; instead, it will have to import the object and then use the desired property. For instance, if I assume you do direclty export cloudFunctions:

...then to import (say) the whatever function, then assuming you used someName as the name of the export:

import { someName } from "./the-module";
const { whatever } = someName;

// ...code using `whatever`...
about 4 years ago · Juan Pablo Isaza 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