qferr / mjml-php

A simple PHP library to render MJML to HTML.
https://qferrer.medium.com/rendering-mjml-in-php-982d703aa703
67 stars 9 forks source link

Unable to compile MJML with BinaryRenderer #10

Closed studiomaiis closed 1 year ago

studiomaiis commented 3 years ago

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 commented 3 years ago

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

studiomaiis commented 3 years ago

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 commented 3 years ago

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

sboerrigter commented 2 years ago

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

studiomaiis commented 2 years ago

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.

sboerrigter commented 2 years ago

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?

qferr commented 2 years ago

@sboerrigter Let me know the error message please.

sboerrigter commented 2 years ago

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 commented 2 years ago

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

sboerrigter commented 2 years ago

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 commented 2 years ago

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

sboerrigter commented 2 years ago
$ npm -v
8.12.1
$ node -v
v16.13.0
qferr commented 2 years ago

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 commented 2 years ago

Can you drop the result of $ which node?

sboerrigter commented 2 years ago

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

qferr commented 2 years ago

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

sboerrigter commented 2 years ago

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 commented 2 years ago

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

qferr commented 2 years ago

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 commented 1 year ago

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

marcbelletre commented 1 year ago

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?