Unexpected token '<' when export email
Describe the Bug
In development environment, the rendering works properly like this:
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
- Install packages (
pnpm install --frozen-lockfile) - Run export command (
pnpm export)
Expected Behavior
email should be exported
What's your node version? (if relevant)
v20.7.0
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.
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 {};
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'. 😞
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.
Going to close this as a duplicate of #639.
FYI, adding "jsx": "react-jsx" to my Node.js tsconfig.json fixed this issue for me.