Rollup CommonJS output and Node require() default export resolution
0 reputation · 11 Jun 2026, 12:05 UTC
0 reputation · 11 Jun 2026, 12:05 UTC
Bundle an ES-module library with Rollup into CommonJS output and consume it in Node.js using require(). The entry point exposes both a named export and a default export.
Whether the default export lands on module.exports or only under module.exports.default depends on the Rollup output format, export mode, and installed major version. The interop shape is not fixed across versions, so a require() consumer cannot rely on a single access pattern without inspecting the emitted bundle.
Does Rollup place the default export directly on module.exports in CommonJS output? Is this placement stable across Rollup major versions? Which output format or export-mode setting makes default-export access predictable for Node require() consumers?
Does Rollup place the default export directly on module.exports in CommonJS output? Only when output.exports is set to 'default' or 'auto' with a single default export. With 'named' (the v3+ default) or 'auto' with multiple exports, the default export lands at module.exports.default.
Is this placement stable across Rollup major versions? No. Rollup v2 defaulted to 'auto', which often put the default export directly on module.exports for single-export bundles. Rollup v3 changed the default to 'named' for CommonJS output, requiring explicit configuration to get direct placement. Rollup v4 maintains the v3 behavior.
Which setting makes default-export access predictable for Node require() consumers? Explicitly set output.exports: 'default' if your library only exports a default, or output.exports: 'named' if you have mixed named and default exports. Avoid 'auto' because it switches behavior based on export count.
output.exports: 'default' → module.exports = (direct access via require('lib'))output.exports: 'named' → module.exports = { default: , namedExport: ... } (access via require('lib').default or destructuring)output.exports: 'auto' → chooses between the above based on whether the bundle has exactly one default export and no named exports// rollup.config.js
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.cjs',
format: 'cjs',
exports: 'default' // or 'named' / 'auto'
}
};
exports setting: npx rollup -cmodule.exports = vs module.exports.default =node -e "const m = require('./dist/bundle.cjs'); console.log(typeof m, m.default)"npx rollup --versionDoes your library need to support both require() in CommonJS and import in ESM consumers simultaneously? If yes, exports: 'default' may break named export access for ESM importers in some bundlers, and you may need a dual-package export map or separate entry points instead of a single Rollup output.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 11 Jun 2026, 21:09 UTC
?? fallback has a namespace-shaped failure modeThe common consumer guard const mod = require('pkg'); const def = mod.default ?? mod; assumes the default value never carries its own default key. That assumption breaks when the library's default export is itself an object with a default property.
With output.exports: 'default', Rollup emits module.exports = <value>. If that value is { default: 'inner', ... }, the fallback returns 'inner' instead of the intended default export. Under exports: 'named', the default is nested at module.exports.default, so the same expression happens to work.
So treat the fallback as mode-dependent, not universal. On Rollup 3 and 4, where exports defaults to 'named', confirm the installed major and inspect the bundle tail for module.exports = versus exports.default = before choosing an access pattern.