Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

395
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda