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

179
Vistas
Migrating a CommonJS 'module.exports = function()' to ESM

I am using NodeJS and have been writing JavaScript for a few years now and am still learning.

For my CJS modules, I write (what I call) a root function that contains all of (what I call) my sub-functions and then return {subfunction1, subfunction2} on the root function for the functions I wanted to expose. Admittedly, I learned this writing style from Jonathan Mills and have been happy with it.

I am struggling with how to migrate this properly from CommonJS to ESM and am hoping to do so without using a Class. However, if Class is the right way with ESM, then I will adapt.

Here is a CJS Service:

service.js

function WebexService(webex) {
  async function processMessage(messageData) {
    try {
      const user = await webex.people.get(messageData.personId);
      //debug(user);
      sendMessage({ displayName: user.displayName, roomId: messageData.roomId });
    } catch (error) {
      debug(error);
      throw error;
    }
  }
  function sendMessage(messageInfo) {
    webex.messages.create({
      roomId: messageInfo.roomId,
      text: `Howdy! ${messageInfo.displayName}`,
    });
  }
  return { processMessage }
}

module.exports = WebexService()

To use the this CJS service, I would import it as:

app.js

const { processMessage } = require('../services/webexService');

function superCool() {
  const messageResponse = await processMessage(messageData);
}

The only way I have been able to get this to work with ESM is as a Class:

service.js

import debugInit from 'debug';
import chalk from 'chalk';
const debug = debugInit('app:services:webex');

export default class WebexService {
  constructor(webex) {
    this.webex = webex;
  }
  async processMessage(messageData) {
    try {
      const user = await this.webex.people.get(messageData.personId);
      //debug(user);
      this.sendMessage({ displayName: user.displayName, roomId: messageData.roomId });
    } catch (error) {
      debug(error);
      throw error;
    }
  }
  sendMessage(messageInfo) {
    this.webex.messages.create({
      roomId: messageInfo.roomId,
      text: `Howdy! ${messageInfo.displayName}`,
    });
  }
}

app.js

import WebexService from '../services/webex.js';

const WebexServiceInstance = new WebexService(webex);
WebexServiceInstance.processMessage(event.data);

I am hopeful someone can point me in the right direction. I'm happy to RTFM if someone can help me find one to read.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

service.js

function WebexService(webex) {
  async function processMessage(messageData) {
    try {
      const user = await webex.people.get(messageData.personId);
      //debug(user);
      sendMessage({ displayName: user.displayName, roomId: messageData.roomId });
    } catch (error) {
      debug(error);
      throw error;
    }
  }
  function sendMessage(messageInfo) {
    webex.messages.create({
      roomId: messageInfo.roomId,
      text: `Howdy! ${messageInfo.displayName}`,
    });
  }
  return { processMessage }
}

const service = WebexService()
export default service

app.js

import WebexService from '../services/webex.js';
// you can then do this
const { processMessage } = WebexService;
function superCool() {
  const messageResponse = await processMessage(messageData);
}
// or just use it directly
function superCool() {
  const messageResponse = await WebexService.processMessage(messageData);
}
about 4 years ago · Juan Pablo Isaza 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