#✅ Error when loading custom operation

11 messages · Page 1 of 1 (latest)

thorn maple
#

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.

lucid coyoteBOT
#

Thanks for posting! This is a community powered server, so you may or may not get an answer based on available help and expertise. To increase your chances of somebody being able to help you, please help us help you making sure you:

  • Adding an explanation of exactly what you're trying to achieve.
  • Adding any and all related code or previous attempts.
  • Describing the exact issue or error you are facing.
  • Posting any screenshots if applicable.
  • Reading through https://stackoverflow.com/help/how-to-ask.

When you're done with this thread, please close it. Thanks! ✨

(If you have a support agreement and need help, please contact the core team via email.)

north rune
#

Please make sure you have "type": "module", in your extensions package.json to build for ESM

thorn maple
#

Yup, I do have that in my package.json file

north rune
#

Oh hmm, is one of your dependencies not ESM compatible? Then you'll ahve to remove type: module and rename the output extension to .cjs

thorn maple
#

That's what I tried as well, and then it gives me the following error:

err: {
      "type": "Error",
      "message": "Cannot find module '../lib/utils.js'\nRequire stack:\n- <package path>/mjml-email/dist/api.cjs",
      "stack":
          Error: Cannot find module '../lib/utils.js'
          Require stack:
          - <package path>/mjml-email/dist/api.cjs
              at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
              at Function.resolve (node:internal/modules/cjs/helpers:116:19)
              at <package path>/mjml-email/dist/api.cjs:162628:14
              at Object.<anonymous> (<package path>/mjml-email/dist/api.cjs:162735:3)
              at Module._compile (node:internal/modules/cjs/loader:1254:14)
              at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
              at Module.load (node:internal/modules/cjs/loader:1117:32)
              at Module._load (node:internal/modules/cjs/loader:958:12)
              at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
              at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
      "code": "MODULE_NOT_FOUND",
      "requireStack": [
        "<package path>/mjml-email/dist/api.cjs"
      ]
    }

which confuses me even more

north rune
#

weird, looks like your extension is still doing require('../lib/utils.js') by relative path somewhere which will never work in the final result 🤔

#

don't see it in the operation code shared so must be a dependency

thorn maple
#

yeah, seems to be occuring the moment I import either mjml or liquidjs... Had hoped at least liquidjs would work since Directus seems to use that in the email service as well to render templates

thorn maple
#

Just quickly wanted to update before I closed this. It was indeed an issue with dependencies.

Apparently mjml depends on a package that doesn't work when building it into the operation: https://github.com/mjmlio/mjml/issues/2132

Seems that they will fix this with the next version of mjml, but there's not been a lot of news about it.

GitHub

Describe the bug When importing mjml into an esbuild project the build works fine, but the compiled app has this error: Error: Cannot find module '../lib/utils.js' Full trace: Error: Cannot...

lucid coyoteBOT
#

✅ Error when loading custom operation