Skip to content

vp test fails with ws CJS named export error when nitro.preset is cloudflare_module #831

@Charles5277

Description

@Charles5277

Describe the bug

When using import { ... } from 'vite-plus/test' in a Nuxt project with nitro.preset: 'cloudflare_module' (or 'cloudflare_pages'), vp test run fails with:

SyntaxError: Named export 'WebSocketServer' not found. The requested module 'ws' is a CommonJS module, which may not support all module.exports as named exports.

The same test passes when:

  • Using import { ... } from 'vitest' instead of 'vite-plus/test'
  • Removing the nitro.preset (default or 'node')

The Cloudflare nitro preset appears to change how Nuxt/Nitro resolves modules (likely via unenv or Cloudflare polyfills), causing ws to be loaded through Node's native ESM import() instead of vite-plus's module transform pipeline. Node's native ESM import cannot destructure CJS named exports from ws.

Note: server.deps.inline: ['ws'] in vitest.config.ts does NOT fix this.

Reproduction

https://github.com/Charles5277/viteplus-ws-repro

Steps to reproduce

git clone https://github.com/Charles5277/viteplus-ws-repro.git
cd viteplus-ws-repro
pnpm install
npx vp test run        # ❌ fails with ws CJS error

To verify:

# Remove cloudflare preset → passes
sed -i '' '/preset/d' nuxt.config.ts
npx vp test run        # ✅ passes

System Info

vp v0.1.11

Local vite-plus:
  vite-plus  v0.1.11

Tools:
  vite             v8.0.0
  rolldown         v1.0.0-rc.9
  vitest           v4.1.0
  oxfmt            v0.40.0
  oxlint           v1.55.0
  oxlint-tsgolint  v0.16.0
  tsdown           v0.21.2

Node.js: v24.11.1
macOS Darwin 25.3.0

Used Package Manager

pnpm

Logs

SyntaxError: Named export 'WebSocketServer' not found. The requested module 'ws' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'ws';
const { WebSocketServer } = pkg;

 ❯ VitestModuleEvaluator.runExternalModule node_modules/.pnpm/@voidzero-dev+vite-plus-test@0.1.11_.../dist/module-evaluator.js:80:23
 ❯ VitestModuleRunner.directRequest node_modules/.pnpm/@voidzero-dev+vite-plus-core@0.1.11_.../dist/vite/node/module-runner.js:1206:18
 ❯ test/example.test.ts:1:1

Validations

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Priority

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions