Node mailer giving error while using with hydrogen #3032
-
I am trying to integrate nodemailer in hydrogen but getting this error
Below is the code
If some one have a solution please reply. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, all just a quick reminder if any one have any answer for this. |
Beta Was this translation helpful? Give feedback.
-
Hello, This error occurs because Hydrogen is designed to run in environments like MiniOxygen or Cloudflare Workers, which differ fundamentally from Node.js. These platforms do not support Node.js built-in modules or the CommonJS module pattern ( Recommended Approach: To send transactional emails from a Hydrogen application running on MiniOxygen or Cloudflare Workers, it is best to use an email provider that exposes an HTTP API—such as Mailgun, Postmark, SendGrid, or Resend—and interact with the service using Example: Sending Email with Mailgun via fetch in a Hydrogen/Cloudflare Worker Action export async function action({ context, request }) {
const formData = await request.formData();
const data = Object.fromEntries(formData);
if (data.sendOtp) {
const response = await fetch('https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages', {
method: 'POST',
headers: {
Authorization: 'Basic ' + btoa('api:YOUR_API_KEY'),
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
from: 'noreply@yourdomain.com',
to: data.emailAddress,
subject: 'OTP for Affiliated Programme',
text: `Your OTP is 123123`,
}),
});
if (!response.ok) {
throw new Response('Failed to send OTP', { status: 500 });
}
return { success: true, message: 'OTP sent successfully' };
}
}
Alternative (if you must use Nodemailer): If your project requirements necessitate Nodemailer or other Node.js libraries, the only viable option is to handle email delivery in a separate Node.js backend or serverless function (e.g. AWS Lambda, Vercel Serverless Function). Your Hydrogen app can call this backend via HTTP as needed. |
Beta Was this translation helpful? Give feedback.
Hello,
This error occurs because Hydrogen is designed to run in environments like MiniOxygen or Cloudflare Workers, which differ fundamentally from Node.js. These platforms do not support Node.js built-in modules or the CommonJS module pattern (
require
) and instead rely entirely on ES modules (import
/export
). Even if you resolve the import issue, Nodemailer and similar packages depend on APIs that are simply not available in these edge/serverless contexts.Recommended Approach:
To send transactional emails from a Hydrogen application running on MiniOxygen or Cloudflare Workers, it is best to use an email provider that exposes an HTTP API—such as Mailgun, Postmark, SendGrid, or Resend—and …