qferr / mjml-php

A simple PHP library to render MJML to HTML.

Home Page:https://qferrer.medium.com/rendering-mjml-in-php-982d703aa703

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to compile MJML with BinaryRenderer

studiomaiis opened this issue · comments

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.

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

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.

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

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

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?

@sboerrigter Let me know the error message please.

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!

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

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">
  [...]

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

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

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?

Can you drop the result of $ which node?

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

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

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.

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

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.

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

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?