Skip to content

pg bundling isuses with nitro v3 and cloudflare #4010

Closed as not planned
Closed as not planned

Description

@medz

Describe the feature

While using Nitro, the current bundler has encountered many strange issues, such as:

  • WASM cannot extend beyond the project scope.

  • The runtime requires WASM, but it doesn't handle platform-compliant formats well.

  • External non-standard ESM dependencies (e.g., Prisma ORM, many of its dependencies are CJS, or pg packages, whose ESM support requires writing an ESM file that depends on internal CJS).

  • Non-standard Node module dependencies, such as require("events").

These problems often occur with Cloudflare workers, and updating the current bundling configuration makes it difficult to handle various situations (usually occurring in other dependent packages).

Through testing, wrangler's built-in bundling handles all platform-compliant situations. For example, Hono is clever in that it doesn't handle bundling itself, so using Hono in the same situation works without problems.

For example, a recent problem I encountered involved needing to depend on Prisma ORM and then using the pg driver. Bundling was flawless, but deployment to Cloudflare failed.


Q: So, what is pure packaging mode support?

Simply put, Nitro doesn't handle any packaging; it simply writes the code from a virtual directory to a specific folder and provides an entry file. Alternatively, Nitro provides a virtual entry point (which the user imports and then re-exports).

This way, all code dependencies are located in the original TS/JS files and the pkg folder under node_modules.

Major Benefits

Cloudflare deployment will no longer be hindered; for example, features like cloudflare.exports.ts can be omitted.

Implementation Difficulty

Not yet investigated, but based on previous understanding of Nitro's internal packaging:

  1. You still need to run nitro build.

  2. Set builder in nitro.config.ts to false to support pure packaging mode.

Additional information

  • Would you be willing to help implement this feature?

Activity

  1. pi0 commented on Feb 8, 2026

    @pi0
    Member

    Thanks for the writeup.

    Nitro uses rollup, rolldown or vite for bundling production code and there might be bundling issues, so i appreciate any reproduction of the issues you are mentioning above.

    (also please mention if you are using nitro v3 with rolldown or older nitropack v2 with rollup)

    There is a new standard preset that writes just a fetch export handler woth no platform handling but i don’t suppose in larger projects, wrangler (esbuild) would handle dependencies better than rolldown. (also note that node polyfills in wrangler is based on unjs/unenv that nitro uses)

  2. changed the title [-]Purified packaging mode support[/-] [+](v3) Purified packaging mode support[/+] on Feb 9, 2026
  3. medz commented on Feb 9, 2026

    @medz
    ContributorAuthor

    @pi0 Yes, Wrangler (esbuild) isn't as powerful as Vite/rolldown. However, for NPM packages with Cloudflare-compatible configurations, Wrangler is faster.

    Indeed, Wrangler has made many rule optimizations for Cloudflare compatibility. I've only looked at one fetch preset, but I haven't verified it yet; I need to try it out. Because Nitro's greatest appeal is its filesystem routing.

    I'm using Nitro v3 + Rolldown

  4. medz commented on Feb 9, 2026

    @medz
    ContributorAuthor
    Image Image Image

    This results in issues where, if pg is used and Nitro v3 + Rolldown is used for packaging, the build process is successful, but deployment on Cloudflare fails.

    Verified:

    1. Deployment with pure TS dependencies + Wrangler (using esbuild for packaging) works without problems.
    2. Deployment with Nitro v3 + Rolldown + Wrangler compiles successfully, but deployment fails.
  5. medz commented on Feb 9, 2026

    @medz
    ContributorAuthor

    I'll write a minimal, reproducible repository later ❤️

  6. pi0 commented on Feb 9, 2026

    @pi0
    Member

    This results in issues where, if pg is used and Nitro v3 + Rolldown is used for packaging,

    Thanks, lets investigate this 👍🏼

  7. changed the title [-](v3) Purified packaging mode support[/-] [+]`pg` bundling isuses with nitro v3 and cloudflare\[/+] on Feb 9, 2026
  8. changed the title [-]`pg` bundling isuses with nitro v3 and cloudflare\[/-] [+]`pg` bundling isuses with nitro v3 and cloudflare[/+] on Feb 9, 2026
  9. medz commented on Feb 10, 2026

    @medz
    ContributorAuthor

    @pi0 I prepared a minimal reproduction repo for this issue: https://github.com/medz/nitro-issue-4010-pg-cloudflare-repro

    It includes two script paths:

    1. Nitro + Wrangler (fails)
    • pnpm run repro:nitro:fail
    • nitro build succeeds, but wrangler dev --config .output/server/wrangler.json fails at worker startup with:
      Calling requirefor "events" in an environment that doesn't expose therequire function.
    1. Direct Wrangler (succeeds)
    • pnpm run repro:wrangler:ok
    • wrangler dev runs directly from server.ts and responds with 200, and wrangler deploy --dry-run also succeeds.

    So this looks isolated to the Nitro + rolldown bundling path, not direct Wrangler packaging.

  10. medz commented on Feb 10, 2026

    @medz
    ContributorAuthor

    @pi0 I prepared a second repo as an independent PoC for the issue-body idea:

    https://github.com/medz/nitro-issue-4010-builder-false-prototype

    This PoC validates the builder: false intent:

    • Nitro keeps framework/runtime generation (filesystem routing + virtual modules + worker entry generation).
    • Nitro does not bundle app dependencies.
    • Wrangler/esbuild performs the final bundling for Cloudflare Worker.

    Verification command:

    • pnpm run prototype:verify

    What it checks:

    • prototype:build generates .output/server/index.mjs + virtual/runtime modules.
    • wrangler dev starts successfully from generated output.
    • /api/pg (filesystem route) returns 200 and loads pg.
    • wrangler deploy --dry-run succeeds.

    So the proposal seems technically feasible and meaningful: preserve Nitro DX while delegating packaging compatibility to Wrangler.

  11. medz commented on Mar 20, 2026

    @medz
    ContributorAuthor

    I've released a new package to solve this problem 👉 https://github.com/medz/nitro-wrangler

    Its sole purpose is to ignore user-intentioned code and let wrangler handle the code packaging. It's now officially in use and working great 🫡

  12. pi0 commented on Mar 20, 2026

    @pi0
    Member

    There is no current issue with pg bundling BTW. Depending on multi phase building (esbuild) is not good for long term we should find and fix root cause issues with rolldown setup.

  13. medz commented on Mar 20, 2026

    @medz
    ContributorAuthor

    There is no current issue with pg bundling BTW. Depending on multi phase building (esbuild) is not good for long term we should find and fix root cause issues with rolldown setup.

    @pi0 Yes, the packaging is working fine. However, it's been moved to the runtime. This is more frustrating and harder to debug than a build failure.

    This is just a temporary solution for Nitro to address the compatibility issues of mixed CJS/ESM packaging; please don't reject it outright. It will be archived once Nitro resolves these issues.

    But we are currently experiencing these problems, and configuring these behaviors from Rolldown is extremely cumbersome (we've tried for a month without successfully handling edge cases in various dependencies).

    I think we could create a new issue report from this, specifically regarding the compatibility regressions between dependencies like pg (which internally wraps CJS in ESM to disguise it as ESM) and worked.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions