resend-node: Passing the React email component into the react property throws an error during the next build.

Using the latest Next.js 14.0.1 and resend 2.0.0-canary.2 throws an error when passing the React email template component the react property inside the resend.emails.send function.

Failed to compile.

./node_modules/html-to-text/lib/html-to-text.mjs + 24 modules
Cannot get final name for export 'encodeXML' of ./node_modules/entities/lib/esm/index.js


> Build failed because of webpack errors
   Creating an optimized production build 

This doesn’t work:

export const sendQuotationEmail = async (formData: FormInputs) => {
  const result = QuotationFormSchema.safeParse(formData);
  let data;

  if (result.success) {
    const { name, email, phone, website, service, message } = result.data;

    try {
      data = await resend.emails.send({
        from:
          process.env.EMAIL_FROM_ADDRESS ||
          "Contact Form <onboarding@resend.dev>",
        to: process.env.EMAIL_TO_ADDRESS || "",
        subject: "Message from quotation form",
        reply_to: email,
        react: QuotationFormEmail({
          name,
          email,
          phone,
          website,
          service,
          message,
        }),
      });

  } catch (error: unknown) {
      console.log(error);
    }
  }
};

This works:

import { renderAsync } from "@react-email/render";

export const sendQuotationEmail = async (formData: FormInputs) => {
  const result = QuotationFormSchema.safeParse(formData);
  let data;

  if (result.success) {
    const { name, email, phone, website, service, message } = result.data;

    const html = await renderAsync(
      QuotationFormEmail({
        name,
        email,
        phone,
        website,
        service,
        message,
      }) as React.ReactElement,
    );

    try {
      data = await resend.emails.send({
        from:
          process.env.EMAIL_FROM_ADDRESS ||
          "Contact Form <onboarding@resend.dev>",
        to: process.env.EMAIL_TO_ADDRESS || "",
        subject: "Message from quotation form",
        reply_to: email,
        html: html,
      });

  } catch (error: unknown) {
      console.log(error);
    }
  }
};

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 20 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Can confirm this is also happening for me. I’m on Next 14.0.1 and resend 2.0.0

Using renderAsync as the OP mentioned is the workaround. My send method is nothing special:

const html = await renderAsync(MagicLinkEmail({ url, host }));

    await resend.emails.send({
      from: env.RESEND_EMAIL_FROM,
      to: params.identifier,
      subject: `Log in to ${host}`,
      html: html,
    });

Passing react did work locally but fails on build and receive the encodeXML error. It builds fine when using html using the renderAsync part.

facing the same error as well – thought it was an issue with @react-email/render, but looks like it’s an issue with resend itself

I was running into the same error when building my Next.js project:

Error

> next build

  ▲ Next.js 14.2.3
  - Environments: .env

   Creating an optimized production build ...

./node_modules/html-to-text/lib/html-to-text.mjs + 24 modules
Cannot get final name for export 'encodeXML' of ./node_modules/entities/lib/esm/index.js


> Build failed because of webpack errors

Solution

To fix this, you need to replace the consolidated react-email component imports with individual ones:

Before ❌

import {
  Body,
  Button,
  Container,
  Head,
  Heading,
  Hr,
  Html,
  Img,
  Link,
  Preview,
  Section,
  Text,
} from "@react-email/components";

After ✅

  1. First, install the individual components as separate packages:
npm install @react-email/head @react-email/html @react-email/container @react-email/section @react-email/img @react-email/heading @react-email/text @react-email/button @react-email/hr @react-email/preview @react-email/body @react-email/link
  1. Then update the imports in your file:
import { Html } from "@react-email/html";
import { Head } from "@react-email/head";
import { Preview } from "@react-email/preview";
import { Body } from "@react-email/body";
import { Container } from "@react-email/container";
import { Section } from "@react-email/section";
import { Img } from "@react-email/img";
import { Heading } from "@react-email/heading";
import { Text } from "@react-email/text";
import { Button } from "@react-email/button";
import { Hr } from "@react-email/hr";
import { Link } from "@react-email/link";

Explanation

The consolidated @react-email/components package seems to have issues with webpack’s module resolution, specifically with the entities package’s ESM exports. By importing each component directly from its dedicated package, we avoid these resolution issues while maintaining the same functionality.

Additional Notes

If you’re using additional components like Column, Row, or Tailwind (e.g. because you’re using a different react-email template), you’ll need to install and import those separately as well.

Install

npm install @react-email/column @react-email/row @react-email/tailwind

and import in your code:

import { Column } from "@react-email/column";
import { Row } from "@react-email/row";
import { Tailwind } from "@react-email/tailwind";