MJML Browser Build

5.0.1 · active · verified Sun Apr 19

MJML Browser (`mjml-browser`) is the client-side build of the MJML framework, designed for rendering responsive email templates directly within web browsers. This package, currently stable at version 5.0.1, allows developers to integrate MJML compilation into front-end applications, email builders, or interactive preview environments without requiring a Node.js backend. The MJML project maintains an active release cadence, frequently publishing alpha and beta versions alongside stable updates, indicating ongoing development and feature enhancements. While providing the core MJML rendering capabilities, `mjml-browser` differentiates itself from the full `mjml` Node.js package by its strict browser-only execution environment. This means certain server-side features, such as `mj-include` for modular templating and `.mjmlconfig` for custom components, are intentionally unsupported and ignored. Version 5 introduced significant changes, including a switch to `htmlnano` and `cssnano` for optimized HTML and CSS minification, which may affect output formatting compared to previous major versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart compiles a simple MJML template into responsive HTML directly in the browser, demonstrating the core `mjml2html` function with basic options and error handling.

var mjml2html = require('mjml-browser');

const mjmlTemplate = `
  <mjml>
    <mj-body>
      <mj-section background-color="#f0f0f0">
        <mj-column>
          <mj-text font-size="24px" color="#333" align="center">
            Welcome to MJML Browser!
          </mj-text>
          <mj-image width="150px" src="https://mjml.io/assets/img/logo-mjml-compact.png" alt="MJML Logo" />
          <mj-button href="https://mjml.io" background-color="#414141" color="#ffffff" font-size="16px" padding="10px 25px">
            Learn More About MJML
          </mj-button>
          <mj-text font-size="14px" color="#666" padding-top="20px">
            This demonstrates client-side MJML rendering. Remember, features like
            <code>mj-include</code> and <code>.mjmlconfig</code> for custom components are not supported
            in this browser-specific build. The output is responsive HTML suitable for email clients.
          </mj-text>
        </mj-column>
      </mj-section>
    </mj-body>
  </mjml>
`;

const options = {
  // Options for MJML compilation can be passed here.
  // 'validationLevel' can be 'skip', 'soft', or 'strict'.
  validationLevel: 'strict',
  // Other options specific to server-side Node.js MJML like 'filePath'
  // are not applicable to mjml-browser.
};

try {
  const result = mjml2html(mjmlTemplate, options);

  if (result.errors.length > 0) {
    console.warn("MJML compilation encountered warnings/errors:", result.errors);
  }

  // In a browser context, you would typically inject `result.html`
  // into an iframe or a designated div for preview.
  console.log("Successfully compiled MJML. Generated HTML (truncated):\n", result.html.substring(0, 700) + "...");

  // Example of displaying in a browser element:
  // document.getElementById('email-preview').innerHTML = result.html;

} catch (e) {
  console.error("An error occurred during MJML compilation:", e);
}

view raw JSON →