Skip to content

[v3] Using nitro/vite with cloudflare preset fails to start #3637

Closed

Description

@franklin-tina

Environment

Node v24
MacOS 26 (Tahoe)
Nitro v3

// package.json
{
  "name": "cloudflare-bug",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "build": "vite build",
    "preview": "vite preview",
    "dev": "vite dev"
  },
  "devDependencies": {
    "@tailwindcss/vite": "^4.1.14",

    // I tried using npm:nitro-nightly@beta as it is in the playground but it fails with:
    // error: Package "nitro-nightly" with tag "beta" not found, but package exists
    "nitro": "npm:nitro-nightly@latest",

    "tailwindcss": "^4.1.14",
    "vite": "^7.1.8",
    "wrangler": "^4.42.1"
  }
}

Reproduction

https://codesandbox.io/p/devbox/2dgt9d?migrateFrom=n67zym

Describe the bug

In v3, when using the cloudflare_module preset in dev mode, the server fails to start with the following error:

Error: Failed to load url /project/workspace/node_modules/nitro/dist/runtime/plugin.dev (resolved id: /project/workspace/node_modules/nitro/dist/runtime/plugin.dev) in virtual:. Does the file exist?

The vite config looks like:

// vite.config.ts
import { nitro } from "nitro/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [nitro({
    config: {
      preset: 'cloudflare_module'
    }
  })],
});

Additional context

No response

Logs

Activity

  1. added
    pending triage
    on Oct 9, 2025
  2. pi0 commented on Oct 9, 2025

    @pi0
    Member

    Should be solved by a85ebaa

  3. added a commit that references this issue on Oct 9, 2025
    a85ebaa
  4. franklin-tina commented on Oct 9, 2025

    @franklin-tina
    ContributorAuthor

    Should be solved by a85ebaa

    Thanks @pi0 💚

  5. medz commented on Oct 9, 2025

    @medz
    Contributor
    Image
    "nitro": "npm:[email protected]"
    

    @franklin-tina @pi0 Is yours intact? I have the same problem. I can use vite build without any problems, but vite dev cannot run.

    Error: Failed to load url /Users/seven/workspace/panews-server/universal/node_modules/nitro/dist/runtime/plugin.dev (resolved id: /Users/seven/workspace/panews-server/universal/node_modules/nitro/dist/runtime/plugin.dev) in virtual:. Does the file exist?
        at loadAndTransform (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/chunks/dep-Chhhsdoe.js:26136:33) {
      code: 'ERR_LOAD_URL',
      runnerError: Error: RunnerError
          at reviveInvokeError (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:474:64)
          at Object.invoke (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:547:11)
          at ModuleRunner.getModuleInformation (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:1053:7)
          at request (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:1070:83)
          at /Users/seven/workspace/panews-server/universal/plugins/virtual::2:3
          at ESModulesEvaluator.runInlinedModule (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:906:3)
          at ModuleRunner.directRequest (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:1113:59)
          at ModuleRunner.cachedRequest (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:1031:73)
          at /Users/seven/workspace/panews-server/universal/node_modules/nitro/dist/runtime/internal/app.mjs:5:1
          at ESModulesEvaluator.runInlinedModule (file:///Users/seven/workspace/panews-server/universal/node_modules/vite/dist/node/module-runner.js:906:3)
    }
    
  6. medz commented on Oct 9, 2025

    @medz
    Contributor
    Image
  7. franklin-tina commented on Oct 9, 2025

    @franklin-tina
    ContributorAuthor

    @medz Not quite, I still get the error.
    [email protected]

    I might just wait a bit more (fortunately, it's not super urgent for me)

  8. medz commented on Oct 9, 2025

    @medz
    Contributor

    @franklin-tina Then I spent over four hours downgrading to nitro v2. 😂

  9. franklin-tina commented on Oct 9, 2025

    @franklin-tina
    ContributorAuthor

    @franklin-tina Then I spent over four hours downgrading to nitro v2. 😂

    Sorry about that 🫂
    For my case, I was doing a POC demo that I needed v3 for... hence why I said it wasn't super important.
    I still use v2 (via Nuxt) with no issues.

  10. reopened this on Oct 9, 2025
  11. pi0 commented on Oct 9, 2025

    @pi0
    Member

    Nitro v3 is (pre)alpha state for now, and this issue is specific to cloudflare dev emulation (you can only set preset for production builds).

    Commit the above solved issue when trying in a monorepo but a reliable reproduction could be really helpful.

  12. added
    needs reproduction
    on Oct 9, 2025
  13. franklin-tina commented on Oct 9, 2025

    @franklin-tina
    ContributorAuthor

    Nitro v3 is (pre)alpha state for now, and this issue is specific to cloudflare dev emulation (you can only set preset for production builds).

    Commit the above solved issue when trying in a monorepo but a reliable reproduction could be really helpful.

    I'll try to see if I can get more info on this.
    However, I updated the codesandbox with the latest changes v3.0.0-20251009-155627-a7e0f55d and it still fails.
    And you're right, when I run the build command, and start it with npx wrangler --cwd .output/ dev the build doesn't break.

    @medz Will need your help too if you can reproduce this.
    I think it will be easier for Pooya then

  14. medz commented on Oct 12, 2025

    @medz
    Contributor
    import { nitro } from 'nitro/vite';
    import { defineConfig } from 'vite';
    
    export default defineConfig({
      plugins: [nitro()],
      nitro: { preset: 'cloudflare-module' },
    });

    Change the preset to cloudflare-module, And run vite dev

  15. medz commented on Oct 12, 2025

    @medz
    Contributor

    Or

    import { defineNitroConfig } from 'nitro/config';
    
    export default defineNitroConfig({ preset: 'cloudflare-module' });
    {
      "name": "nitro-app",
      "private": true,
      "scripts": {
        "build": "nitro build",
        "dev": "nitro dev",
        "preview": "npx srvx --prod .output/"
      },
      "devDependencies": {
        "nitro": "latest"
      }
    }
  16. medz commented on Oct 12, 2025

    @medz
    Contributor

    @pi0 @franklin-tina

    I fixed:

    import path from 'node:path';
    import { nitro } from 'nitro/vite';
    import { defineConfig } from 'vite';
    
    export default defineConfig({
      plugins: [
        nitro(),
        {
          name: 'nitro-plugin-dev-resolve',
          resolveId(id: string) {
            if (id.includes('nitro/dist/runtime/plugin.dev')) {
              return path.resolve(
                __dirname,
                './node_modules/nitro/dist/presets/cloudflare/runtime/plugin.dev.mjs',
              );
            }
    
            return void 0;
          },
        },
      ],
      nitro: { preset: 'cloudflare-module' },
    });

    This is the essence of the problem: https://github.com/nitrojs/nitro/blob/main/src/presets/cloudflare/dev.ts#L81

    I wrote a simple resolveId vite plugin to modify it to the correct file address.

    Image
  17. franklin-tina commented on Oct 12, 2025

    @franklin-tina
    ContributorAuthor

    Here is what I did (In the nitro repo), and it seems to run the playground without failing.

    Note

    I haven't fully tested this, since I'm still trying out other solutions 🙈

    cc @pi0 @medz

    In src/presets/cloudflare/dev.ts

    - import { resolve } from "node:path";
    + import { resolve, dirname } from "node:path";
    import { promises as fs } from "node:fs";
    import { fileURLToPath } from "mlly";
    import type { Nitro } from "nitro/types";
    + import { presetsDir, pkgDir } from "nitro/runtime/meta";
    import { findFile } from "pkg-types";
    import { resolveModulePath } from "exsolve";
    
    export async function cloudflareDevModule(nitro: Nitro) {
      // ...existing code
    
      // Make sure runtime is transpiled
      nitro.options.externals.inline = nitro.options.externals.inline || [];
    -  nitro.options.externals.inline.push(
    -    fileURLToPath(new URL("runtime/", import.meta.url))
    -  );
    
    -  // Add plugin to inject bindings to dev server
    -  nitro.options.plugins = nitro.options.plugins || [];
    -  nitro.options.plugins.push(
    -    fileURLToPath(new URL("runtime/plugin.dev", import.meta.url))
    -  );
    
    + const pluginPath = await findExistingPath([
    +    resolve(pkgDir, "src/presets/cloudflare/runtime/plugin.dev.ts"),
    +    resolve(presetsDir, "cloudflare/runtime/plugin.dev.mjs"),
    +    resolve(presetsDir, "cloudflare/runtime/plugin.dev.js"),
    +    resolve(presetsDir, "cloudflare/runtime/plugin.dev.ts"),
    +    fileURLToPath(new URL("runtime/plugin.dev.mjs", import.meta.url)),
    +    fileURLToPath(new URL("runtime/plugin.dev.js", import.meta.url)),
    +    fileURLToPath(new URL("runtime/plugin.dev.ts", import.meta.url)),
    +  ]);
    
    +  if (!pluginPath) {
    +    nitro.logger.warn(
    +      "Failed to locate Cloudflare dev runtime plugin. Bindings injection will be skipped."
    +    );
    +    return;
    +  }
    
    +  const runtimeDirPath = dirname(pluginPath);
    +  nitro.options.externals.inline.push(runtimeDirPath);
    
      // Add plugin to inject bindings to dev server
      nitro.options.plugins = nitro.options.plugins || [];
    - nitro.options.plugins.push(
    -   fileURLToPath(new URL("runtime/plugin.dev", import.meta.url))
    - );
    +  nitro.options.plugins.push(pluginPath);
    }
    
    + async function findExistingPath(paths: string[]) {
    +  for (const path of paths) {
    +    try {
    +      await fs.access(path);
    +      return path;
    +    } catch {}
    +  }
    + }

    In src/presets/cloudflare/runtime/plugin.dev.ts:

    - // @ts-ignore
    - import { useRuntimeConfig, getRequestURL } from "#imports";
    + import { useRuntimeConfig } from "../../../runtime/internal/runtime-config";
    
    
    async function _getPlatformProxy() {
      const _pkg = "wrangler"; // Bypass bundling!
    -  const { getPlatformProxy } = (await import(_pkg).catch(() => {
    +  const { getPlatformProxy } = (await import(/* @vite-ignore */ _pkg).catch(() => {
        throw new Error(
          "Package `wrangler` not found, please install it with: `npx nypm@latest add -D wrangler`"
        );
      })) as typeof import("wrangler");
    }
    Image
  18. pi0 commented on Oct 13, 2025

    @pi0
    Member

    Ah thanks we had another regression. 3afbc40 should fix it

  19. added a commit that references this issue on Oct 13, 2025
    3afbc40
  20. franklin-tina commented on Oct 13, 2025

    @franklin-tina
    ContributorAuthor

    Thanks @pi0 @medz
    I can confirm the issue no longer persists (Nitro version 3.0.1-20251013-093643-3afbc402)

    ♥️

  21. medz commented on Oct 13, 2025

    @medz
    Contributor

    Great! Once this fix is ​​released into a future version, I can delete the following code.

    Image
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

    needs reproductionpending triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions