Home > Enterprise >  Migrating a CommonJS 'module.exports = function()' to ESM
Migrating a CommonJS 'module.exports = function()' to ESM

Time:07-05

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.

CodePudding user response:

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);
}
  • Related