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

399
Visualizações
Is there a way to create a module that can be consumed by both ESM and CJS applications?

I have a backend CJS application & a frontend ESM application. And a node module I have created in ESM. The node module works fine for my ESM application because they are both using ESM import syntax. Trying to consume it in the CJS application obviously throws errors as it cannot read the ESM import syntax.

I've tried using Rollup.js to convert the ESM code into CJS and using conditional exports in my package.json file but this does not work.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Assuming it is a library like:

my-library.js

// No require on top. They will be local to their use.
const myLibrary = ...

// export default instead of module.exports
export default myLibrary

The library must first have its extension rename to .mjs ("my-library.js" = "my-library.msj")

Then,

in CJS (Node.js) =>

Dynamic import:

const {default: myUtilityLib } = await import("./my-library.mjs");

In ESM (Browser) =>

Standard import as usual:

import myUtilityLib from "./my-library.mjs"
over 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