sighmon / mjml-rails

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

MJML not found with NVM setup #83

Open AlecRust opened 3 years ago

AlecRust commented 3 years ago

Background

After a format of my machine and careful new setup via my dotfiles, I have Node installed only via NVM (no duplicate Homebrew Node version at /usr/local/node) and have installed global Node packages using npm with the recommended approach. This includes Yarn, which they recommend installing via npm.

Point being I'm fairly confident I have a "clean" Node setup with no duplicates, everything installed as-per docs and scoped to the chosen NVM Node version.

Problem

I have mjml in my project's package.json and have ran yarn.

"mjml": "^4.9.3",

But I get this error from mjml-rails:

Couldn't find the MJML 4. binary.. have you run $ npm install mjml?

Even if make MJML a global package with npm install -g mjml I get the same error.

I'm not sure if local project npm install mjml works, but we use Yarn instead. Does mjml-rails rely on local project npm usage instead of Yarn?

sighmon commented 3 years ago

@AlecRust I don't use yarn, so I've never tested mjml-rails functionality with it.

But other contributors have added yarn support, here's where it's checked: https://github.com/sighmon/mjml-rails/blob/master/lib/mjml.rb#L67

Looking at the way we look for a valid yarn mjml binary, can you see anywhere where it might be failing for your setup?

  def self.check_for_yarn_mjml_binary
    yarn_bin = `which yarn`.chomp
    return unless yarn_bin.present?
    mjml_bin = "#{yarn_bin} run mjml"
    return mjml_bin if check_version(mjml_bin)
  end
AlecRust commented 3 years ago

Thanks @sighmon. I see that code runs which yarn to find the binary. I assume this is the issue, as which yarn in my case doesn't return a path to a binary, but a function:

yarn () {
    unset -f changelog emoj eslint gulp lungo ncu node nodemon npm npm-check npm-check-updates npx retry serve splash stylelint yarn yarnpkg yo yo-complete nvm > /dev/null 2>&1
    _zsh_nvm_load
    yarn "$@"
}

This is standard based on the NVM/npm installed Yarn setup described above, however sometimes when I run which yarn I do get a binary path:

/Users/alec/.nvm/versions/node/v16.2.0/bin/yarn

Looks like the check code would work for a Homebrew or manual Yarn installation which adds /usr/local/bin/yarn, but not with the recommended approach of installing yarn with npm install --global yarn, when using NVM.

AlecRust commented 3 years ago

OK, figured out what is causing this. I had export NVM_LAZY_LOAD=true in my ~/.zshrc which is an option of zsh-nvm.

I have disabled this, but I don't seem to have this issue with other gems or Node packages. A very crude solution would be to include this command in your gem before the code that runs which yarn:

source "$NVM_DIR/nvm.sh"