Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to compile MJML with BinaryRenderer #10

Closed
studiomaiis opened this issue Sep 17, 2021 · 21 comments
Closed

Unable to compile MJML with BinaryRenderer #10

studiomaiis opened this issue Sep 17, 2021 · 21 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@studiomaiis
Copy link

Hi !

I'm trying to use this library, but I'm getting a Fatal error :

Fatal error: Uncaught RuntimeException: Unable to compile MJML. Stack error: The command "'/Users/maiis/Sites/mjml/node_modules/.bin/mjml' '-i' '-s' '--config.validationLevel' '--config.minify'" failed. Exit Code: 127(Command not found) Working directory: /Users/maiis/Sites/mjml/wp-content/themes/client Output: ================ Error Output: ================ env: node: No such file or directory in /Users/maiis/Sites/mjml/wp-content/themes/client/inc/commun/emails/lib/mjml/vendor/qferr/mjml-php/src/Mjml/Renderer/BinaryRenderer.php:49 Stack trace: #0 /Users/maiis/Sites/mjml/wp-content/themes/client/index.php(16): Qferrer\Mjml\Renderer\BinaryRenderer->render('<mjml>\n\t<mj-hea...') #1 /Users/maiis/Sites/mjml/wp-includes/template-loader.php(106): include('/Users/maiis/Si...') #2 /Users/maiis/Sites/mjml/wp-blog-header.php(19): require_once('/Users/maiis/Si...') #3 /Users/maiis/Sites/atelierd in /Users/maiis/Sites/mjml/wp-content/themes/client/inc/commun/emails/lib/mjml/vendor/qferr/mjml-php/src/Mjml/Renderer/BinaryRenderer.php on line 49

When I run from the CLI node_modules/.bin/mjml test-std.mjml -o output.html it works, when I use the ApiRenderer it works too.

Any ideas why the command can't run ?

Thanks.

@qferr
Copy link
Owner

qferr commented Sep 17, 2021

Hi! It looks like node is not installed where your app is hosted:

Error Output: ================ env: node: No such file or directory in /Users/maiis/Sites/mjml/wp-content/themes/client/inc/commun/emails/lib/mjml/vendor/qferr/mjml-php/src/Mjml/Renderer/BinaryRenderer.php:49

Sometimes you need to do a symlink: https://stackoverflow.com/a/26320915

@qferr qferr self-assigned this Sep 17, 2021
@qferr qferr added the help wanted Extra attention is needed label Sep 17, 2021
@studiomaiis
Copy link
Author

Thank you for this quick answer.

Node is installed, node -v gives me v16.9.1. From the node_modules/.bin/ folder node mjml "works" as it returns me

Command line error:
No input argument received

I've tried sudo ln -s "$(which node)" /usr/bin/node as it was suggested on the link you provided (Mac OS 11.5.2 node installed via homebrew), but I get a ln: /usr/bin/node: Operation not permitted error.

I'm sorry, if it's 100% node related let's close this thread.

@qferr
Copy link
Owner

qferr commented Sep 17, 2021

Alright. Keep me informed, I'll reopen if necessary.

@qferr qferr closed this as completed Sep 17, 2021
@sboerrigter
Copy link

Same issue here. Do you remember how you solved this?

@studiomaiis
Copy link
Author

studiomaiis commented Jul 12, 2022 via email

@sboerrigter
Copy link

Ok, thanks for your quick reply. Unfortunately the API is no option for me because it uses an outdated version of MJML that has trouble rendering responsive e-mails in Outlook (mjmlio/mjml#2434).

@qferr maybe this issue should be reopened?

@qferr
Copy link
Owner

qferr commented Jul 14, 2022

@sboerrigter Let me know the error message please.

@sboerrigter
Copy link

I have the same error as above:

PHP Fatal error:  Uncaught Qferrer\Mjml\Exception\ProcessException: env: node: No such file or directory
 in [...]/vendor/qferr/mjml-php/src/Process/Process.php:42
Stack trace:
#0 [...]/vendor/qferr/mjml-php/src/Renderer/BinaryRenderer.php(42): Qferrer\Mjml\Process\Process->run()
#1 [...]/my-repo/MyClass.php(85): Qferrer\Mjml\Renderer\BinaryRenderer->render('<mjml owa='desk...')

I tested this on my local development environment (MacOS with Laravel Valet, Nginx, PHP 8.0) and the production server (Linux, Apache, PHP 8.0). They both give the same error. Also if I try to render some very basic markup like this:

$renderer = new \Qferrer\Mjml\Renderer\BinaryRenderer(dirname(__DIR__, 3) . '/node_modules/.bin/mjml');
$html = $renderer->render('
    <mjml>
        <mj-body>
            <mj-section>
                <mj-column>
                    <mj-text>Hello World</mj-text>
                </mj-column>
            </mj-section>
        </mj-body>
    </mjml>
');

I hope this helps. Just let me know if there is anything I can do to help!

@qferr
Copy link
Owner

qferr commented Jul 14, 2022

Are you sure node is installed correctly? What happens if you run node_modules/.bin/mjml command locally? Does it works?

@sboerrigter
Copy link

That works:

$ node_modules/.bin/mjml input.mjml
<!-- FILE: input.mjml -->
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
  <title>
  </title>
  <!--[if !mso]><!-->
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <!--<![endif]-->
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  [...]

@qferr
Copy link
Owner

qferr commented Jul 14, 2022

Can you try npm -v and node -v from the project directory please?

@sboerrigter
Copy link

$ npm -v
8.12.1
$ node -v
v16.13.0

@qferr
Copy link
Owner

qferr commented Jul 19, 2022

I ran unit tests and a PHP test file with the example from the README.md, I can't reproduce the problem. Did you try to run a PHP file test with the example?

@qferr qferr reopened this Jul 19, 2022
@qferr
Copy link
Owner

qferr commented Jul 19, 2022

Can you drop the result of $ which node?

@sboerrigter
Copy link

~/.nvm/versions/node/v16.13.0/bin/node

@qferr
Copy link
Owner

qferr commented Jul 21, 2022

I think node should be in /usr/bin or /usr/local/bin.

Could you add a symlink ln -s /Users/xxx/.nvm/versions/node/v16.13.0/bin/node /usr/bin/node?

Source : nvm-sh/nvm#786

@sboerrigter
Copy link

I cannot add a symlink to /user/bin/node that gives me this error (also if i add sudo):

ln: /usr/bin/node: Operation not permitted

I can add it to /user/local/bin/node. But that doesn't solve the issue unfortunately.

I am not using NVM on my production server which has the same problem.

@sboerrigter
Copy link

Are there any updates on this or is there anything else I can do to help? Thanks in advance.

@qferr
Copy link
Owner

qferr commented Sep 1, 2022

Hello. I can't reproduce the problem. I think your infrastructure is the cause. The automated tests passed and I tested this on my local development and it works well.

@qferr
Copy link
Owner

qferr commented Mar 20, 2023

If you still have the problem, please reopen the issue.

@qferr qferr closed this as completed Mar 20, 2023
@marcbelletre
Copy link

Hi there,

I struggled all day with the exact same issue. Using the command from a terminal worked like a charm but I got the same error when running the command from the server.

This answer on StackOverflow finally got me to the solution.

When running the command through Laravel you may have to set the path where Node is installed as an environment var. In my case it was /usr/local/bin/

Here is a simple wrapper that takes an MJML file as argument and renders the HTML:

<?php

namespace App\Support;

use Illuminate\Support\Facades\Process;

class Mjml
{
    /**
     * Render HTML from an MJML file.
     */
    public static function render(string $path): string
    {
        $env = [];

        if (config('settings.node_path')) {
            $env['PATH'] = '$PATH:'.config('settings.node_path');
        }

        $result = Process::path(base_path())
            ->env($env)
            ->run("node_modules/.bin/mjml {$path} -s")
            ->throw();

        return $result->output();
    }
}

Here is my settings.php file:

<?php

return [
    // ...

    'node_path' => env('NODE_PATH', null),
];

Finally you can set the Node path in your .env file:

NODE_PATH=/usr/local/bin/

Maybe this plugin could include an option to set this value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants