Skip to content

Can't use optional dynamic import with Cloudflare presets #3903

Closed

Description

@gabrielmfern

Environment

Nitro: 2.12.9
Node.js: 24.12.0

export default defineNitroConfig({
  compatibilityDate: '2025-03-01',
  srcDir: 'server',
  preset: 'cloudflare-module',
  cloudflare: {
    deployConfig: true,
    nodeCompat: true,
  },
});

Reproduction

https://stackblitz.com/edit/github-6rbpfq8q?file=server%2Froutes%2Findex.ts&view=editor

To reproduce:

  1. Go the URL above
  2. Run npm run build

Describe the bug

Running build with any Cloudflare preset and a dynamic import, such as:

try {
    await import('doesnt-exist');
} catch {
    console.log('this is fine');
}

Errors saying it can't resolve doesnt-exist.

Generally bundlers have a proper handling for dynamic imports with try-catches around them, and normally Nitro does indeed handle those properly from what I've seen, but when using the Cloudflare presets it doesn't seem to work.

Additional context

This is originally an issue in compatibility with Resend's Node SDK resend/resend-node#772.

Logs

> nitro build

✔ Generated public .output/public                                                      nitro 9:15:40 AM
ℹ Building Nitro Server (preset: cloudflare-module, compatibility date: 2025-02-23)    nitro 9:15:41 AM

[nitro 9:15:42 AM]  ERROR  Error: Cannot resolve "@react-email/render" from "/home/projects/vbwijkajko.github/server/routes/index.ts" and externals are not allowed!


undefined


[9:15:42 AM]  ERROR  Cannot resolve "@react-email/render" from "/server/routes/index.ts" and externals are not allowed!

    at Object.resolveId (node_modules/nitropack/dist/rollup/index.mjs:2026:17) 



[9:15:42 AM]  ERROR  Cannot resolve "@react-email/render" from "/server/routes/index.ts" and externals are not allowed!

Activity

  1. coderabbitai commented on Dec 29, 2025

    @coderabbitai
    Contributor
    🔗 Similar Issues

    Related Issues

    🔗 Related PRs

    #3799 - refactor!: overhaul externals support [merged]
    #3834 - feat(cloudflare): support exports.cloudflare.ts [merged]
    #3854 - refactor(vite): keep SSR bundle external dependencies [merged]
    #3877 - refactor: improve vfs [merged]
    #3886 - fix: use module condition and main fields for bundling [merged]

    👤 Suggested Assignees

    Enable issue planning

    To enable issue planning, add the following to your .coderabbit.yaml:

    issue_enrichment:
      planning:
        enabled: true

    You can then request a plan by commenting @coderabbitai plan on any issue.

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. serhii-chernenko commented on Feb 2, 2026

    @serhii-chernenko

    had the same. as a temp solution:

    "peerDependencies": {
    	"@react-email/render": "^2.0.4"
    },
    "peerDependenciesMeta": {
    	"@react-email/render": {
    		"optional": true
    	}
    },
  3. gabrielmfern commented on Feb 2, 2026

    @gabrielmfern
    Author

    @serhii-chernenko Where did you add that to? Your project?

    Changing the version of @react-email/render fixed it for you?

  4. serhii-chernenko commented on Feb 2, 2026

    @serhii-chernenko

    @gabrielmfern not changing version. just added to peerDependencies in package.json.

    I'm upgrading packages right now in my repo:
    https://github.com/besidka/besidka/commits/main/

    It's not pushed yet but will do soon

  5. added
    bugSomething isn't working
    and removed on Sep 2, 2026
  6. pi0x commented on Sep 7, 2026

    @pi0x
    Contributor

    Fixed in v3 — a try/catch dynamic import of a missing module builds fine with the cloudflare-module preset now that externals handling was overhauled (#3799), and the import is left as-is in the output.

    Triaged with AI assistance.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingv2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions