mjml: Cannot find module '../lib/utils.js' in esbuild

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 find module '../lib/utils.js'
Require stack:
- /Users/thom/develop/laboratory/mjml-build-error/out.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
    at Function.resolve (node:internal/modules/cjs/helpers:98:19)
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:39836:13
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:11:5
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:39919:18
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:11:5
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:47259:23
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:11:5
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:51912:43
    at /Users/thom/develop/laboratory/mjml-build-error/out.js:11:5 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/thom/develop/laboratory/mjml-build-error/out.js' ]
}

To Reproduce I’ve uploaded a very simple working example just follow README to run:

https://drive.google.com/uc?id=1Xfb9d1jEJ9nIOyI8Tqnm6tuRkydRKdb3&export=download

Expected behavior The app should execute without errors.

MJML environment (please complete the following information):

  • OS: Mac OS 11.1 Big Sur
  • MJML Version 4.8.1
  • MJML tool used: npm package

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Hi I’ve found a workaround inspired by https://github.com/mjmlio/mjml/issues/1365#issuecomment-555835755. Basically, i defined a plugin that would return an empty module for the uglify node file:

let emptyMjmlUglifyPlugin = {
  name: 'empty mjml uglify plugin',
  setup(build) {
    build.onLoad({ filter: /uglify-js\/tools\/node.js$/ }, () => ({
      contents: '{}',
      loader: 'js',
    }));
  },
};

tested with mjml 4.11.0

For anyone trying to use MJML in AWS Lambda bundled with CDK (which uses esbuild):

@sadtomatoofjoy workaround cannot be used because CDK doesn’t provide the ability to define custom esbuild plugins (it is used in CLI, not in Node). I got it working by adding uglify-js in the nodeModules property.

It increases the code size indeed, but it’s the best I found.