mjml-php icon indicating copy to clipboard operation
mjml-php copied to clipboard

Unable to compile MJML with BinaryRenderer

Open studiomaiis opened this issue 4 years ago • 19 comments
trafficstars

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.

studiomaiis avatar Sep 17 '21 09:09 studiomaiis

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 avatar Sep 17 '21 09:09 qferr

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.

studiomaiis avatar Sep 17 '21 10:09 studiomaiis

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

qferr avatar Sep 17 '21 11:09 qferr

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

sboerrigter avatar Jul 12 '22 13:07 sboerrigter

Nope, I'm using their API.

Le 12 juil. 2022 à 15:13, Sjoerd Boerrigter @.***> a écrit :

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

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

studiomaiis avatar Jul 12 '22 16:07 studiomaiis

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 (https://github.com/mjmlio/mjml/issues/2434).

@qferr maybe this issue should be reopened?

sboerrigter avatar Jul 12 '22 17:07 sboerrigter

@sboerrigter Let me know the error message please.

qferr avatar Jul 14 '22 08:07 qferr

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!

sboerrigter avatar Jul 14 '22 09:07 sboerrigter

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

qferr avatar Jul 14 '22 09:07 qferr

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

sboerrigter avatar Jul 14 '22 09:07 sboerrigter

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

qferr avatar Jul 14 '22 09:07 qferr

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

sboerrigter avatar Jul 14 '22 10:07 sboerrigter

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 avatar Jul 19 '22 10:07 qferr

Can you drop the result of $ which node?

qferr avatar Jul 19 '22 10:07 qferr

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

sboerrigter avatar Jul 21 '22 07:07 sboerrigter

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 : https://github.com/nvm-sh/nvm/issues/786

qferr avatar Jul 21 '22 08:07 qferr

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 avatar Jul 25 '22 08:07 sboerrigter

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

sboerrigter avatar Aug 29 '22 15:08 sboerrigter

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 avatar Sep 01 '22 11:09 qferr

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

qferr avatar Mar 20 '23 07:03 qferr

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?

marcbelletre avatar Jun 20 '23 15:06 marcbelletre