Skip to content

RSC: cjs to esm interop helper doesn't handle native/external cjs import properly #1087

@ErleiX

Description

@ErleiX

Related plugins

Describe the bug

Description

The current CJS_INTEROP_HELPER in the CJS module runner transform doesn't properly handle built-in Node modules or external CJS modules.

Current Behavior

The current implementation only checks if a module was transformed by this plugin:

```javascript
function cjs_interop(m) {
return m.__cjs_module_runner_transform ? m.default : m;
}
```

Problem

This is a minimal repro example

const EventEmitter = require('events');

class CustomEventEmitter extends EventEmitter {
}

where the transformed esm result would be something like the following

function __cjs_interop__(m) { return m.__cjs_module_runner_transform ? m.default : m; }

const EventEmitter = (__cjs_interop__(await import('events')));

class CustomEventEmitter extends EventEmitter {
}

when the transformed esm js file being evaluated, Node complains about EventEmitter because it's the namespace object not a constructor

Class extends value [object Module] is not a constructor or null
         at eval (...\node_modules\@repo\custom-event\index.js:3:27)
         at async ESModulesEvaluator.runInlinedModule (file:///.../node_modules/vite/dist/node/module-runner.js:913:3)

Reproduction

https://github.com/ErleiX/vite-plugin-rsc-repro/

Steps to reproduce

follow the repro steps in the readme file of the repro repo I link above

System Info

System:
    OS: Windows 11 10.0.26200
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1265U
    Memory: 3.83 GB / 31.83 GB
  Binaries:
    Node: 24.13.0 - C:\nvm4w\nodejs\node.EXE
    npm: 11.6.2 - C:\nvm4w\nodejs\npm.CMD
  Browsers:
    Chrome: 144.0.7559.110
    Edge: Chromium (142.0.3595.94)
  npmPackages:
    @vitejs/plugin-react: ^5.1.2 => 5.1.2
    @vitejs/plugin-rsc: ^0.5.15 => 0.5.17
    vite: ^7.3.1 => 7.3.1

Used Package Manager

npm

Logs

Class extends value [object Module] is not a constructor or null
at eval (...\node_modules@repo\custom-event\index.js:3:27)
at async ESModulesEvaluator.runInlinedModule (file:///.../node_modules/vite/dist/node/module-runner.js:913:3)

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions