sighmon / mjml-rails

MJML + ERb/Haml/Slim view template
https://mjml.io
Other
295 stars 64 forks source link

Prevents booting when `which` is missing #108

Open coder2000 opened 1 year ago

coder2000 commented 1 year ago

Current version apparently uses which to locate the mjml binary, however if it is not installed on the system the app will not boot and throw an exception.

sighmon commented 1 year ago

@coder2000 Does this config work for your use case? Which OS are you using?

# config/initializers/mjml.rb
Mjml.setup do |config|
  config.mjml_binary = "/path/to/custom/mjml"
end
johannesschobel commented 7 months ago

i think the issue is related to using yarn instead of npm.

I have the same issue - if i use yarn add mjml the binaries somehow are not found. If i use npm install mjml, everything works as expected

sighmon commented 7 months ago

@johannesschobel Oh interesting. Is there something that can be done to fix it?

johannesschobel commented 7 months ago

Actually, I don't know yet.. I will try to set up a reproduction repository in order to isolate this issue. Will need some time for this. Maybe next week. I would like to rule other possible issues out, because I only tried this in my real application.

I will come back to this issue with more information and try to explain the steps.

All the best

johannesschobel commented 7 months ago

ok.. i think, i got one step closer.. in a "brand new repository(runrails new NAME,bundle add mjml-rails,yarn add mjml) and then start the application viarails s` works perfectly..

This left me puzzled, as i thought that this may be the issue..

Running yarn run mjml-cli --version also outputs the correct version numbers. so this should work.

Then i tried the same command yarn run mjml-cli --version in my production application (where this error occurs) - and this fails with the following error:

yarn run mjml-cli --version
yarn run v1.22.17
$ /home/schobel/projects/rails/XXX/node_modules/.bin/mjml-cli --version
/home/schobel/projects/rails/XXX/node_modules/string-width/index.js:2
const stripAnsi = require('strip-ansi');
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/schobel/projects/rails/XXX/node_modules/strip-ansi/index.js from /home/schobel/projects/rails/XXX/node_modules/string-width/index.js not supported.
Instead change the require of /home/schobel/projects/rails/XXX/node_modules/strip-ansi/index.js in /home/schobel/projects/rails/XXX/node_modules/string-width/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/schobel/projects/rails/XXX/node_modules/string-width/index.js:2:19)
    at Object.<anonymous> (/home/schobel/projects/rails/XXX/node_modules/cliui/build/index.cjs:291:21)
    at Object.<anonymous> (/home/schobel/projects/rails/XXX/node_modules/yargs/build/index.cjs:2861:12)
    at Object.<anonymous> (/home/schobel/projects/rails/XXX/node_modules/yargs/index.cjs:5:30)
    at Object.<anonymous> (/home/schobel/projects/rails/XXX/node_modules/mjml-cli/lib/client.js:34:37)
    at Object.<anonymous> (/home/schobel/projects/rails/XXX/node_modules/mjml-cli/bin/mjml:3:1) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.17.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So the issue points to the require(...) statement in other javascript libraries.. damn, how i hate CJS / ESM conflicts. I then manually changed the mentioned require(...) statements to import(...) statements (you only need to change "require" to "import" - thats all) and rerun yarn run mjml-cli --version and it now works.

Need to dig a bit deeper.. But these are my results so far.. Hope this helps someone a bit further. Will post updates again.

All the best, Johannes

johannesschobel commented 7 months ago

if you then run npm install just once, it works.. i really don't know why.. you can then even remove the package-lock.json file.. and completely remove the node_modules folder.. and re-install via yarn and it works from now on..

dont ask?!

johannesschobel commented 7 months ago

In another github issue (cannot find the link) they mentioned, that this may be an issue related to an old version of yarn (1.x). I noticed, that if you update yarn via npm, it stays on the old version, as yarn itself is now "separated" from npm.

Note, that the default Dockerfile shipped with rails 7.1 relies on yarn 1.22.17 - which is quite old.

Issue could also be related to an old yarn version?!

I will try, if this is the case

johannesschobel commented 7 months ago

i ended up running a

npm install -g mjml

in my Dockerfile that creates the image for my server to get rid of the issue. However, obviously, i am looking for a better solution ;)