After upgrading to Vite 8, `import Recur from 'recur-rules'` (a CommonJS package compiled from ESM) in an app with `"type": "module"` yields an object holding `default`, not the function; why, and what fixes it?
answer
- the default import of CommonJS
- who imports, not only who exports
- an .mjs importer or type: module
- __esModule no longer decides it here
- legacy.inconsistentCjsInterop as a bridge
basics
~20 sVite 8 makes the default import of a CommonJS module equal module.exports whenever the importer is .mjs or .mts or its package.json declares type: module, even if the module sets __esModule. Read .default explicitly, or get the package fixed.
solid answer
~40 sVite 8 applies one rule in dev and build. The `default` import of a CommonJS module is `module.exports` if the importer is `.mjs` or `.mts`, if the importer's nearest `package.json` has `"type": "module"`, or if the module does not set `__esModule`; otherwise it is `module.exports.default`. `recur-rules` sets `__esModule` and `exports.default`, but the app's `package.json` declares `"type": "module"`, so the default import is now the whole `module.exports` object, the same answer Node's own ESM loader gives. Before Vite 8 the two importer conditions counted only when the importer was itself part of the dependency optimization, so app code fell through to `__esModule` and got the function. Fix the import site to read `.default` explicitly, ask the package to ship real ESM, and use the deprecated `legacy.inconsistentCjsInterop: true` only as a temporary bridge.
code
javascript · 9 lines// node_modules/recur-rules/index.js (CommonJS compiled from ESM)
Object.defineProperty(exports, '__esModule', { value: true })
exports.default = function Recur(rule) { /* ... */ }
// src/schedule.js (the app's package.json has "type": "module")
import recurModule from 'recur-rules'
// Vite 8: recurModule is module.exports, i.e. { default: [Function: Recur] }
const Recur = recurModule.defaultgo deeper
Recall that a CommonJS module has no real default export, so the bundler must choose between module.exports and module.exports.default.
Explain the three conditions that make the default import module.exports in Vite 8 and why a type: module app now gets the whole object from a package compiled from ESM.
Diagnose the TypeError from the import site, the package's __esModule marker and the importer's format, fix the import or the package, and keep the legacy flag strictly temporary.
Plan the upgrade across many apps: inventory CommonJS packages compiled from ESM, push maintainers toward real ESM builds, and time-box any legacy flag with a dated removal.
## Two readings of one import An ES module's `import x from 'pkg'` asks for the module's **default export**. A CommonJS module has no exports in that sense, only a `module.exports` value, so every tool that lets ESM import CommonJS has to choose what `default` means. The ecosystem has two answers, and the language material on CommonJS interop covers them in depth. In one clause each: - **The transpiler reading.** If the module sets the `__esModule` marker, it was compiled from ESM, so `default` is `module.exports.default`. - **Node's reading.** `default` is always `module.exports`, marker or not. The two agree for a plain CommonJS module and disagree for exactly the kind of package in this question: a library compiled from ESM, which sets `__esModule` and puts its main function on `exports.default`. ## The rule Vite 8 applies Vite 8's migration guide calls the change **consistent CommonJS interop**. The default import is `module.exports` when any of the conditions below holds, and `module.exports.default` otherwise. | Condition (any one is enough) | Default import | |---|---| | The importer is `.mjs` or `.mts` | `module.exports` | | The importer's nearest `package.json` has `"type": "module"` | `module.exports` | | The CommonJS module does not set `__esModule` to `true` | `module.exports` | | None of the above | `module.exports.default` | The rule holds in both places. In development, Vite's import analysis rewrites the default import of a pre-bundled CommonJS dependency according to it. In the build, Rolldown's CommonJS handling applies the same rule. Rolldown's own documentation on bundling CommonJS adds fallbacks for dynamic imports and for a module that sets `__esModule` without having any `default` property. ## Why the upgrade changed the answer Before Vite 8, development and build each had their own rule: - **In development**, the `.mjs` and `type: module` conditions counted only when the importer was *itself included in the dependency optimization*. App source never is, so for app code only `__esModule` mattered. `recur-rules` sets it, so the import got `module.exports.default`, the function. - **In the build**, the conditions were "`__esModule` not set" or "`module.exports` has no `default` property". `recur-rules` has both the marker and the property, so the build also got the function. Vite 8 drops the "included in the optimization" qualifier. The calendar's source files live in a package whose `package.json` says `"type": "module"`, so the import now gets the whole `module.exports` object, something like `{ default: [Function: Recur] }`. The first call to `Recur(...)` throws a `TypeError` because the value is not a function. ## What the rule does not touch The change is narrower than it first looks: - **Named imports.** `import { RRule } from 'recur-rules'` reads a property of `module.exports` in every version, so it is unaffected. - **ESM dependencies.** A package that ships real ES modules has a real default export. No interop rule is involved. - **CommonJS modules without the marker.** If the module never sets `__esModule`, the default import was already `module.exports`, and it still is. - **Importers in a package without `"type": "module"`** that are not `.mjs` or `.mts` keep the marker-based answer. What changes is one combination: an ESM-format importer default-importing a CommonJS module that carries the `__esModule` marker. Packages compiled from ESM to CommonJS are exactly that kind of module. ## Fixes, in order of preference 1. **Fix the import site.** Import the module and read `.default` yourself, as the code example shows. This is also what Node's ESM loader returns for the same import, so the code no longer depends on a bundler's heuristic. 2. **Fix the package.** The migration guide asks you to report an affected package to its author, or send a pull request, linking Rolldown's explanation. A package that ships a real ESM build, or an `exports` map with an `import` condition, never reaches this rule. 3. **Bridge temporarily.** `legacy.inconsistentCjsInterop: true` restores the previous behaviour. It is deprecated and meant for buying time, not for keeping. What not to do: - **Do not remove `"type": "module"`** from the app's `package.json` to dodge the rule. That changes how Node treats every `.js` file in the package, the Vite config included. - **Do not rely on `__esModule` alone** when you predict the result. Under Vite 8 the importer's format comes first. ## How to confirm the diagnosis - Log the imported value. An object whose `default` is the expected function confirms the reading. - Check the package's entry file for the `__esModule` marker and an `exports.default` assignment. - Check the importer: its extension, and the `type` field of its nearest `package.json`. - Flip `legacy.inconsistentCjsInterop` to `true` for a moment. If the function comes back, the interop rule is the cause.
- Why did Vite 7 give the function in both dev and build?In dev, the `.mjs` and `type: module` conditions applied only to importers that were themselves part of the dependency optimization, so app code fell back to the `__esModule` check and got `module.exports.default`. In the build, `module.exports` counted only when `__esModule` was unset or no `default` property existed, and `recur-rules` has both, so the build got the function as well.
- Would the same import behave differently in an app without `"type": "module"`?Yes. A `.js` importer whose nearest `package.json` has no `type` field meets none of the `module.exports` conditions, because `recur-rules` sets `__esModule`, so the default import is `module.exports.default`, the function. An `.mjs` or `.mts` importer would still get `module.exports`, whatever its `package.json` says.
- What exactly does `legacy.inconsistentCjsInterop: true` restore?Vite's behaviour before version 8: in dev the importer-based conditions count only for importers inside the dependency optimization, and the build uses its own `__esModule`-and-`default` check. It is deprecated, a bridge while a package or an import site is fixed, not a setting to keep.
- In Vite 8 the __esModule flag alone decides what a CommonJS default import returns.
- Vite 8 still resolves CommonJS default imports differently in dev and in build.
- Removing "type": "module" from the app's package.json is the clean fix.
- legacy.inconsistentCjsInterop is a lasting option for keeping the old behaviour.
- The change is a Vite 8 bug, because a default import must return exports.default.
Assumes Vite 8: its consistent CommonJS interop replaced Vite 7's separate dev and build rules; legacy.inconsistentCjsInterop restores the old behaviour temporarily.