I've been trying to build a custom Flow operation that uses MJML to render email templates.
The development and build process all seem to work, but when I install the extension locally (via npm install ../<folder>), I get the following error:
[12:21:37.412] WARN: Couldn't register operation "directus-extension-mjml-email-operation"
[12:21:37.413] WARN: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '<package path>/mjml-email/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
I tried building it with the .cjs extension, but that throws different errors.
The only dependencies of the extension are liquidjs and mjml.
export default defineOperationApi<Options>({
id: "mjml-email-operation",
handler: async (
{ template, data, to, subject },
{ services, getSchema, database, accountability, env, exceptions }
) => {
const { InvalidPayloadException } = exceptions;
const { MailService } = services;
const mailService = new MailService({
schema: getSchema({ database }),
accountability,
knex: database,
});
const liquidEngine = new Liquid({
root: [
path.resolve(env["EXTENSIONS_PATH"], "templates"),
path.resolve(__dirname, "templates"),
],
extname: ".liquid",
});
const templatePath = path.resolve(
env["EXTENSIONS_PATH"],
"templates",
`${template}.liquid`
);
if (!(await fse.pathExists(templatePath)) === false) {
throw new InvalidPayloadException(`Template ${template} does not exist`);
}
const templateString = await fse.readFile(templatePath, "utf-8");
const htmlTemplate = mjml2html(templateString).html;
const html = await liquidEngine.parseAndRender(htmlTemplate, data);
await mailService.send({
to,
subject,
html,
});
return {
to,
subject,
template,
data,
};
},
});
Extensions SDK version: 10.1.3
Project version: 10.2.1
I'm currently lost on what could be causing this, so any help would greatly be appreaciated.