[英]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.我正在使用 NodeJS,并且已经编写 JavaScript 几年了,并且仍在学习。
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.对于我的 CJS 模块,我编写(我称之为)一个包含所有(我称之为)我的子函数的根函数,然后在我想要公开的函数的根函数上
return {subfunction1, subfunction2}
。 Admittedly, I learned this writing style from Jonathan Mills and have been happy with it.不可否认,我从Jonathan Mills那里学到了这种写作风格,并且对它很满意。
I am struggling with how to migrate this properly from CommonJS to ESM and am hoping to do so without using a Class
.我正在努力解决如何将其从 CommonJS 正确迁移到 ESM 并且希望在不使用
Class
的情况下这样做。 However, if Class
is the right way with ESM, then I will adapt.但是,如果
Class
是 ESM 的正确方式,那么我会适应。
Here is a CJS Service:这是一个 CJS 服务:
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:要使用此 CJS 服务,我会将其导入为:
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
:我能够让它与 ESM 一起工作的唯一方法是作为一个
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.
如果有人可以帮助我找到一个可以阅读的内容,我很高兴 RTFM。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.