[GH-ISSUE #167] vite dev crashes: ssrLoadModule fails in configureServer when instrumentation.ts exists #38

Closed
opened 2026-05-06 12:36:44 +02:00 by BreizhHardware · 1 comment

Originally created by @southpolesteve on GitHub (Feb 27, 2026).
Original GitHub issue: https://github.com/cloudflare/vinext/issues/167

Description

vite dev crashes during server startup when the project has an instrumentation.ts (or src/instrumentation.ts) file. The configureServer hook calls server.ssrLoadModule() to load instrumentation, but Vite 7's SSRCompatModuleRunner fails because the SSR environment channel is not yet initialized at that point in the server lifecycle.

Error

file:///node_modules/vite/dist/node/chunks/config.js:15041
    options$1.channel.api.outsideEmitter.on("send", onMessage);
                          ^

TypeError: Cannot read properties of undefined (reading 'outsideEmitter')
    at Object.connect (vite/dist/node/chunks/config.js:15041:26)
    at new ModuleRunner (vite/dist/node/module-runner.js:1009:34)
    at new SSRCompatModuleRunner (vite/dist/node/chunks/config.js:15097:3)
    at ssrLoadModule (vite/dist/node/chunks/config.js:15076:36)
    at runInstrumentation (vinext/dist/server/instrumentation.js:61:34)
    at configureServer (vinext/dist/index.js:2101:21)
    at _createServer (vite/dist/node/chunks/config.js:25603:97)

Repro

  1. Create a project with src/instrumentation.ts that exports register() and/or onRequestError()
  2. Run npx vite dev
  3. Server crashes immediately

Environment

  • vinext 0.0.12
  • Vite 7.3.1
  • Node.js v24.3.0 (also reproduced on v22.1.0)
  • Also fails under Bun

Root cause

In packages/vinext/src/index.ts, the configureServer hook calls runInstrumentation(server, instrumentationPath) which invokes server.ssrLoadModule(). In Vite 7, ssrLoadModule creates an SSRCompatModuleRunner that requires the SSR environment's transport channel to be initialized. During configureServer, the channel is not yet set up, so channel.api is undefined.

Workaround

Remove instrumentation.ts to prevent the code path from triggering. This is not viable for projects that need instrumentation (e.g., PostHog, Sentry).

Suggested fix

Defer the runInstrumentation call until after the server is fully initialized, for example by using the server's listen event or moving it into the returned middleware function.

Originally created by @southpolesteve on GitHub (Feb 27, 2026). Original GitHub issue: https://github.com/cloudflare/vinext/issues/167 ## Description `vite dev` crashes during server startup when the project has an `instrumentation.ts` (or `src/instrumentation.ts`) file. The `configureServer` hook calls `server.ssrLoadModule()` to load instrumentation, but Vite 7's `SSRCompatModuleRunner` fails because the SSR environment channel is not yet initialized at that point in the server lifecycle. ## Error ``` file:///node_modules/vite/dist/node/chunks/config.js:15041 options$1.channel.api.outsideEmitter.on("send", onMessage); ^ TypeError: Cannot read properties of undefined (reading 'outsideEmitter') at Object.connect (vite/dist/node/chunks/config.js:15041:26) at new ModuleRunner (vite/dist/node/module-runner.js:1009:34) at new SSRCompatModuleRunner (vite/dist/node/chunks/config.js:15097:3) at ssrLoadModule (vite/dist/node/chunks/config.js:15076:36) at runInstrumentation (vinext/dist/server/instrumentation.js:61:34) at configureServer (vinext/dist/index.js:2101:21) at _createServer (vite/dist/node/chunks/config.js:25603:97) ``` ## Repro 1. Create a project with `src/instrumentation.ts` that exports `register()` and/or `onRequestError()` 2. Run `npx vite dev` 3. Server crashes immediately ## Environment - vinext 0.0.12 - Vite 7.3.1 - Node.js v24.3.0 (also reproduced on v22.1.0) - Also fails under Bun ## Root cause In `packages/vinext/src/index.ts`, the `configureServer` hook calls `runInstrumentation(server, instrumentationPath)` which invokes `server.ssrLoadModule()`. In Vite 7, `ssrLoadModule` creates an `SSRCompatModuleRunner` that requires the SSR environment's transport channel to be initialized. During `configureServer`, the channel is not yet set up, so `channel.api` is undefined. ## Workaround Remove `instrumentation.ts` to prevent the code path from triggering. This is not viable for projects that need instrumentation (e.g., PostHog, Sentry). ## Suggested fix Defer the `runInstrumentation` call until after the server is fully initialized, for example by using the server's `listen` event or moving it into the returned middleware function.
Author
Owner

@illegalcall commented on GitHub (Feb 27, 2026):

Hey @southpolesteve, thanks for filing this. I've been trying to reproduce the crash but haven't been able to trigger it with the setup described.

What I've tried:

  • vinext 0.0.12 + Vite 7.3.1
  • Node 22.14.0, Node 24.14.0, and Bun 1.2.21
  • npm, pnpm, and bun as package managers
  • Pages Router only and App Router (with app/ directory)
  • Symlinked vinext via file: dependency

In all cases, src/instrumentation.ts with a register() export loads fine — no crash.

The only way I could reproduce the exact outsideEmitter error was by explicitly overriding the SSR environment with a plain DevEnvironment:

// vite.config.ts
import { defineConfig, DevEnvironment } from "vite";
export default defineConfig({
  environments: {
    ssr: {
      dev: {
        createEnvironment(name, config) {
          return new DevEnvironment(name, config, {});
        },
      },
    },
  },
});

With the default config, Vite creates a RunnableDevEnvironment for SSR which has hot.api already initialized before configureServer runs — so there's no timing issue.

Could you share your exact reproduction project or vite.config.ts? Specifically:

  1. Do you have any custom environments config or a plugin that overrides the SSR environment?
  2. Is there anything else in your setup beyond what's listed in the issue?

That will help me target the fix correctly.

<!-- gh-comment-id:3975569313 --> @illegalcall commented on GitHub (Feb 27, 2026): Hey @southpolesteve, thanks for filing this. I've been trying to reproduce the crash but haven't been able to trigger it with the setup described. What I've tried: - vinext 0.0.12 + Vite 7.3.1 - Node 22.14.0, Node 24.14.0, and Bun 1.2.21 - npm, pnpm, and bun as package managers - Pages Router only and App Router (with app/ directory) - Symlinked vinext via file: dependency In all cases, src/instrumentation.ts with a register() export loads fine — no crash. The only way I could reproduce the exact outsideEmitter error was by explicitly overriding the SSR environment with a plain DevEnvironment: ``` // vite.config.ts import { defineConfig, DevEnvironment } from "vite"; export default defineConfig({ environments: { ssr: { dev: { createEnvironment(name, config) { return new DevEnvironment(name, config, {}); }, }, }, }, }); ``` With the default config, Vite creates a RunnableDevEnvironment for SSR which has hot.api already initialized before configureServer runs — so there's no timing issue. Could you share your exact reproduction project or vite.config.ts? Specifically: 1. Do you have any custom environments config or a plugin that overrides the SSR environment? 2. Is there anything else in your setup beyond what's listed in the issue? That will help me target the fix correctly.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#38
No description provided.