Skip to content

fix: enable Node.js compatibility for Cloudflare Workers#780

Merged
atinux merged 1 commit intonuxt-hub:mainfrom
onmax:fix/node-compat
Jan 16, 2026
Merged

fix: enable Node.js compatibility for Cloudflare Workers#780
atinux merged 1 commit intonuxt-hub:mainfrom
onmax:fix/node-compat

Conversation

@onmax
Copy link
Copy Markdown
Contributor

@onmax onmax commented Jan 15, 2026

Closes #711

Summary

nodeCompat = false was accidentally shipped in v0.10.1, breaking Node.js crypto APIs on Cloudflare Workers. Password verification with scrypt fails silently.

StackBlitz

Link Expected
Bug nuxthub-711 ❌ crypto stubbed
Fix nuxthub-711-fixed ✅ node:crypto external

CLI Reproduction

git clone --depth 1 --filter=blob:none --sparse https://github.com/onmax/repros.git
cd repros && git sparse-checkout set nuxthub-711
cd nuxthub-711 && pnpm i && pnpm build

Verify fix

git sparse-checkout add nuxthub-711-fixed
cd ../nuxthub-711-fixed && pnpm i && pnpm build

Investigation Details

Git History

The bug originated in commit b00d094 ("chore: try to set compat flag manually") during a debugging session:

38d7ed8 chore: revert compat date
4ae483a chore: try using nitro default with latest compat date
b2cb2fc fix: add no_bundle mode
b00d094 chore: try to set compat flag manually  ← nodeCompat changed to false
96517c5 chore: try bundling again
030a0ce chore(release): v0.10.1  ← shipped broken

Later moved to cloudflare.ts in PR #720 (cec8c89), keeping nodeCompat = false.

The comment says "// Enable Cloudflare Node.js compatibility" but the code disables it.

Why Two Things Are Needed

Cloudflare Workers Node.js compatibility requires two layers:

Layer Config Purpose
Build-time nodeCompat: true Applies Nitro's unenvCfNodeCompat preset, marks node:* as external
Runtime nodejs_compat flag Tells workerd to provide Node.js modules

From Nitro source (src/presets/cloudflare/utils.ts):

export async function enableNodeCompat(nitro: Nitro) {
  nitro.options.cloudflare.nodeCompat ??= true;  // defaults to true
  if (nitro.options.cloudflare.nodeCompat) {
    nitro.options.unenv.push(unenvCfNodeCompat);  // THIS IS SKIPPED when false
  }
}

The unenvCfNodeCompat preset marks node:crypto and other modules as external, so rollup leaves import 'node:crypto' as-is instead of bundling polyfills.

What Went Wrong

NuxtHub was setting:

  • nodeCompat = false → unenv preset NOT applied → rollup bundles broken polyfills
  • nodejs_compat flag in wrangler.json → runtime can provide modules, but bundler already polyfilled them

Result: incomplete crypto polyfills at build time. User @raress96 confirmed: [unenv] crypto.createHash is not implemented yet!

Build Output Comparison

Version nitro.mjs size Reason
Bug (nodeCompat: false) 154 KB Bundles crypto polyfills
Fix (nodeCompat: true) 108 KB node:crypto external

@vercel
Copy link
Copy Markdown

vercel bot commented Jan 15, 2026

@onmax is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 15, 2026

npm i https://pkg.pr.new/@nuxthub/core@780

commit: d3e5cd4

@atinux
Copy link
Copy Markdown
Contributor

atinux commented Jan 16, 2026

Weird because I disabled it to set the compat flag manually, let's try with your fix

@atinux atinux merged commit cd53e00 into nuxt-hub:main Jan 16, 2026
7 of 9 checks passed
@onmax onmax deleted the fix/node-compat branch January 16, 2026 13:40
@raress96
Copy link
Copy Markdown

When will this be included in a new release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞 Bug Report: Scrypt password verification fails in production after upgrading to NuxtHub 0.10.* on Cloudflare

3 participants