react-email icon indicating copy to clipboard operation
react-email copied to clipboard

Unexpected token '<' when export email

Open hiddenest opened this issue 3 years ago • 6 comments

Describe the Bug

In development environment, the rendering works properly like this: 스크린샷 2023-10-01 오후 8 57 33

However, it throws error once you try to export:

> email export --pretty

✔ Preparing files...

⠋ rendering reset-password-en.jsnode:internal/validators:95
      throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received an instance of SyntaxError
    at process.set [as exitCode] (node:internal/bootstrap/node:123:9)
    at Object.exit (node:internal/process/per_thread:188:24)
    at process.<anonymous> (/Users/hiddenest/Documents/me/react-email-module-import-error/node_modules/.pnpm/[email protected]/node_modules/react-email/dist/source/utils/start-server-command.js:43:27)
    at process.emit (node:events:514:28)
    at process.processEmit [as emit] (/Users/hiddenest/Documents/me/react-email-module-import-error/node_modules/.pnpm/[email protected]/node_modules/signal-exit/index.js:199:34)
    at process._fatalException (node:internal/process/execution:159:25)
    at processPromiseRejections (node:internal/process/promises:289:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32) {
  code: 'ERR_INVALID_ARG_TYPE'
}

After adding console.log() to start-server-command.js with the result of code variable, I found that the react-email couldn't understand react component.

⠋ rendering reset-password-en.js/Users/hiddenest/Documents/me/react-email-module-import-error/out/reset-password-en.js:39449
var Head = ({ language, title, children }) => <dist_exports.Head>
                                              ^

SyntaxError: Unexpected token '<'

Which package is affected (leave empty if unsure)

react-email

Link to the code that reproduces this issue

https://github.com/hiddenest/react-email-module-import-error

To Reproduce

  1. Install packages (pnpm install --frozen-lockfile)
  2. Run export command (pnpm export)

Expected Behavior

email should be exported

What's your node version? (if relevant)

v20.7.0

hiddenest avatar Oct 01 '23 12:10 hiddenest

I also had this error, I've been having issues within our repository at the moment with different supported versions of node. Downgrading to v18.17.1 resolved this issue for me. I was using v20.x previously.

Mykhol avatar Oct 02 '23 00:10 Mykhol

Interesting. When I downgraded to 18.18.0 and tested it, I don't get any errors, but the process seems to be killed and the export doesn't happen.

To solve this problem, I temporarily wrote a script to generate HTML manually as shown below:

import path from 'path';
import fs from 'fs/promises';

import { renderAsync } from '@react-email/components';

const ROOT = path.resolve(process.cwd(), './emails');
const DIST = path.resolve(process.cwd(), './out');

const exportEmail = async () => {
  const lists = await fs
    .readdir(ROOT)
    .then((files) => files.filter((file) => file.endsWith('.tsx')));

  await createOutDir();

  await Promise.all(
    lists.map(async (list) => {
      const url = path.resolve(ROOT, list);
      await renderHTML(url);
    }),
  );
};

const createOutDir = async () => {
  try {
    const stats = await fs.stat(DIST);
    if (stats.isDirectory()) {
      return;
    }

    await fs.mkdir(DIST);
  } catch {
    await fs.mkdir(DIST);
  }
};

const renderHTML = async (url: string) => {
  const module = await import(url);
  const filename = path.basename(url, '.tsx');

  try {
    const html = await renderAsync(module.default({}), { pretty: true });
    await fs.writeFile(path.resolve(DIST, `${filename}.html`), html);

    console.log(`✅ Exported ${filename}.html`);
  } catch (error) {
    console.log('-----------------------------------------------------');
    console.log(`❌ Failed to export ${filename}.html`);
    console.error(error);
    console.log('-----------------------------------------------------');
  }
};

exportEmail();

export {};

hiddenest avatar Oct 02 '23 03:10 hiddenest

Hey @hiddenest, I think your script looks great, so I decided to give it a try. Unfortunately, renderAsync is exported by @react-email/render. When I changed the import statement, I encountered the error SyntaxError: The requested module '@react-email/components' does not provide an export named 'Body'. 😞

zeekrey avatar Oct 10 '23 09:10 zeekrey

Looking into this now, This is a bug with the way the export command works. The problem is that we bundle the code temporarily in a .js file to be able to import it without jsx and problems with paths, but what happens is that since it is building typescript, it is building based on the jsx property (which for you guys I suppose is preserve just like the reproduction from @hiddenest) on the tsconfig, thus keeping the jsx that the CLI can't parse. A workaround for now would then be setting the jsx to be react-jsx.

gabrielmfern avatar Nov 10 '23 09:11 gabrielmfern

Going to close this as a duplicate of #639.

gabrielmfern avatar Nov 11 '23 08:11 gabrielmfern

FYI, adding "jsx": "react-jsx" to my Node.js tsconfig.json fixed this issue for me.

phgn0 avatar Nov 05 '24 08:11 phgn0