Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module 'node:util/types' in [email protected] when importing cheerio #1628

Closed
jonluca opened this issue Aug 23, 2024 · 1 comment
Labels
Type: Bug Confirmed bug

Comments

@jonluca
Copy link

jonluca commented Aug 23, 2024

Describe the Bug

We have a post processing step for our rendered emails that parses the HTML and swaps out links for tracking links, like so:

import { renderAsync } from "./email-render/render-async";
import * as cheerio from "cheerio";
import type React from "react";

export const BASE_EMAIL_CLICK_TRACKING_URL = "https://www.ourdomain.com/api/emails/click";
export const BASE_EMAIL_OPEN_TRACKING_URL = "https://www.ourdomain.com/api/emails/open";

const renderEmailTemplate = async (email: React.ReactElement, emailId: string | null) => {
  const renderedEmail = await renderAsync(email, {
    pretty: true,
  });
  if (!emailId) {
    return renderedEmail;
  }
  // Extract links from the rendered email
  const parsedHtml = cheerio.load(renderedEmail);
  parsedHtml("a").each((_, element) => {
    const link = parsedHtml(element).attr("href");
    if (link && !link.startsWith("mailto:") && !link.includes(BASE_EMAIL_OPEN_TRACKING_URL)) {
      const transformedUrl = new URL(BASE_EMAIL_CLICK_TRACKING_URL);
      transformedUrl.searchParams.append("emailId", emailId);
      transformedUrl.searchParams.append("next", link);
      const transformedLink = transformedUrl.toString();
      parsedHtml(element).attr("href", transformedLink);
    }
  });
  // Todo actually figure out the double encoding issue
  return parsedHtml.html().replace(/&next=/gi, "&next=");
};

export default renderEmailTemplate;

// and we use it like so, where EmailTemplate is the react component

const html = await renderEmailTemplate(EmailTemplate, emailId);

In the latest version, we get this exception

CleanShot 2024-08-23 at 10 45 12@2x

Which package is affected (leave empty if unsure)

react-email

Link to the code that reproduces this issue

Seen above

To Reproduce

Import 'cheerio' anywhere in the code base of the dev server

Expected Behavior

Importing cheerio works

What's your node version? (if relevant)

No response

@jonluca jonluca added the Type: Bug Confirmed bug label Aug 23, 2024
@gabrielmfern
Copy link
Collaborator

Fixed it under [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants