Skip to content

vite dev crashes: ssrLoadModule fails in configureServer when instrumentation.ts exists #167

@southpolesteve

Description

@southpolesteve

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions