<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sandeep Singh on Medium]]></title>
        <description><![CDATA[Stories by Sandeep Singh on Medium]]></description>
        <link>https://medium.com/@sandeep_tech?source=rss-e1377527cdcd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tJtQSUDTEM_c8VtrafGvJQ.jpeg</url>
            <title>Stories by Sandeep Singh on Medium</title>
            <link>https://medium.com/@sandeep_tech?source=rss-e1377527cdcd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 03:03:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sandeep_tech/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why Nodemailer Doesn’t Work on Cloudflare Workers (And What To Do Instead)]]></title>
            <link>https://medium.com/@sandeep_tech/why-nodemailer-doesnt-work-on-cloudflare-workers-and-what-to-do-instead-437c3f2a8623?source=rss-e1377527cdcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/437c3f2a8623</guid>
            <category><![CDATA[cloudflare-workers]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[nodemailer]]></category>
            <dc:creator><![CDATA[Sandeep Singh]]></dc:creator>
            <pubDate>Sat, 08 Aug 2026 09:13:28 GMT</pubDate>
            <atom:updated>2026-08-08T09:13:28.502Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>A short explanation of a wall a lot of developers hit, why it isn’t going away, and the five lines that replace it.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xrzRBHpYnvKoj-SQDk8QEg.png" /></figure><p>You wrote a contact form. It worked locally. You deployed it to a Cloudflare Worker, or a Vercel Edge Function, or Deno Deploy, and got something like this:</p><pre>TypeError: Class extends value #&lt;Object&gt; is not a constructor or null</pre><p>Or, if you were luckier and got a useful error:</p><pre>Module not found: Can&#39;t resolve &#39;net&#39;</pre><p>Then you spent an hour trying compatibility flags, polyfills, and bundler aliases. I want to save you the rest of that hour.</p><p>This isn’t a bug, and no amount of configuration will fix it.</p><h3>The actual reason</h3><p>Nodemailer’s default transport is SMTP. SMTP is a protocol that runs over a raw TCP connection. To open one in Node.js, you call net.createConnection().</p><p>Cloudflare Workers don’t run on Node.js. They run on V8 isolates — the same engine as Chrome, without the Node runtime around it. Vercel’s Edge Runtime and Deno Deploy are built on similar principles.</p><p>In that environment, there is no net module, because there are no raw TCP sockets. All networking is handled by managed infrastructure <em>outside</em> the runtime — Cloudflare&#39;s own writeup on bringing node:http to Workers is explicit about this: connection pooling, TLS negotiation, and egress IP management are handled at the system level, which is precisely why a subset of Node APIs can never be supported.</p><p>So the chain is:</p><p><strong>No raw TCP → no </strong><strong>net.createConnection() → no SMTP client → no Nodemailer.</strong></p><p>There’s a second, smaller issue that often gets conflated with this one. <a href="https://github.com/nodemailer/nodemailer/issues/1621">Nodemailer issue #1621</a> points out that Nodemailer imports built-in modules without the node: prefix, which breaks the Workers build step. That one is fixable. But fixing it wouldn&#39;t help — you&#39;d just move the failure from build time to runtime, where net still doesn&#39;t exist. <a href="https://github.com/nodemailer/nodemailer/issues/1623">Issue #1623</a> covers the broader edge-function problem.</p><p>It’s worth being clear that none of this is a knock on Nodemailer. It’s an excellent library, actively maintained, MIT-0 licensed, with zero runtime dependencies and millions of weekly downloads. It does one job extremely well: constructing a correct MIME message and getting it onto a wire. The edge simply doesn’t have the wire.</p><h3>What to do instead</h3><p>Use an HTTP API. Every transactional email provider has one, and HTTP is the one thing edge runtimes are exceptionally good at.</p><p>Here’s a complete Cloudflare Worker that sends email:</p><pre>export default {<br>  async fetch(request, env) {<br>    const res = await fetch(&quot;https://api.yourprovider.com/v1/send&quot;, {<br>      method: &quot;POST&quot;,<br>      headers: {<br>        &quot;Authorization&quot;: `Bearer ${env.EMAIL_API_KEY}`,<br>        &quot;Content-Type&quot;: &quot;application/json&quot;,<br>      },<br>      body: JSON.stringify({<br>        from: &quot;hello@yourdomain.com&quot;,<br>        to: &quot;user@example.com&quot;,<br>        subject: &quot;Hello from the edge&quot;,<br>        html: &quot;&lt;p&gt;It works.&lt;/p&gt;&quot;,<br>      }),<br>    });</pre><pre>    if (!res.ok) {<br>      return new Response(`Send failed: ${res.status}`, { status: 502 });<br>    }<br>    return new Response(&quot;Sent&quot;);<br>  },<br>};</pre><p>That’s it. No dependency. No polyfill. No bundler config.</p><p>Store the key with wrangler secret put EMAIL_API_KEY so it never lands in your repo or your deploy logs.</p><h3>The part nobody says out loud</h3><p><strong>On the edge, you don’t need an email library at all.</strong></p><p>There’s a growing set of runtime-agnostic email libraries built specifically to fill this gap, and they’re well made. But be honest with yourself about what you’re buying. If you’re sending a handful of transactional message types from a Worker, fetch is the whole solution. A wrapper around fetch earns its place when you need provider-swapping, a unified message type across a large codebase, or heavy MIME construction with attachments and inline images.</p><p>For a password reset and a welcome email? Ship the five lines.</p><h3>Runtime cheat sheet</h3><p>Runtime Nodemailer / SMTP What to use Node.js (Express, Fastify, NestJS) ✅ Works Nodemailer, or an HTTP SDK Next.js — route handlers, server actions ✅ Works (Node runtime) Nodemailer, or an HTTP SDK Next.js — Edge Runtime ❌ fetch to an HTTP API Next.js — middleware ❌ (always edge) Don&#39;t send email here at all AWS Lambda, containers ✅ Works Nodemailer, or an HTTP SDK Cloudflare Workers ❌ fetch to an HTTP API Deno Deploy ❌ fetch to an HTTP API Bun ⚠️ Mostly works Test it; fetch is safer</p><p>Two notes on the Next.js rows, because this trips people up constantly.</p><p>Route handlers and server actions run on the <strong>Node.js runtime by default</strong> — Nodemailer works fine there. You only lose it if you’ve explicitly opted into export const runtime = &#39;edge&#39;.</p><p>Middleware is <em>always</em> edge. Even setting aside the runtime, middleware runs on every matched request with tight execution limits. Sending email from it is a bad idea for reasons that have nothing to do with SMTP.</p><h3>While you’re in here: the duplicate email bug</h3><p>This one bites people right after they solve the edge problem, and it’s worth fixing in the same sitting.</p><p>Serverless platforms retry on timeout. If your function calls the email API, the send succeeds, and then the function times out on something downstream, the platform re-invokes it. The email goes out twice.</p><p>Your user gets two password reset emails with two different tokens. Now they’re confused, and one of those tokens is a live credential floating in an inbox.</p><p>The fix is an idempotency key — a stable identifier sent with the request, which the provider uses to deduplicate for some window. Retry with the same key, get the original result back instead of a second send:</p><pre>headers: {<br>  &quot;Idempotency-Key&quot;: `pwreset-${userId}-${requestId}`,<br>}</pre><p>Derive it from something stable about the <em>request</em>, not from Date.now() or a fresh UUID — those change on retry, which defeats the entire mechanism.</p><p>Not every provider supports this. It’s worth checking before you pick one, because you cannot build it yourself in a stateless function. Deduplication requires state, and your function doesn’t have any.</p><h3>Summary</h3><ul><li>Nodemailer can’t run on edge runtimes because those runtimes have no TCP sockets. This is architectural and permanent, not a bug awaiting a fix.</li><li>On the edge, call an HTTP API with fetch. It&#39;s five lines.</li><li>In Node — including most Next.js code — Nodemailer is still great.</li><li>Check your provider supports idempotency keys before serverless retries send your users duplicate emails.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=437c3f2a8623" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>