When importing a CommonJS module and assigning to a property on module.exports, Rolldown generates code that fails at runtime. This reproduction uses the debug package as an example, but the issue affects any CJS module where you need to assign to a property on module.exports.
TypeError: Cannot set property log of [object Object] which has only a getter
Rolldown generates inconsistent access patterns when wrapping CJS modules for ESM:
- For function calls: correctly uses
import_src.default() - For property assignments: incorrectly uses
import_src.prop = ...
The ESM wrapper object created by __toESM has getter-only properties, so direct assignment fails.
Expected: import_src.default.prop = ... (consistent with function calls)
pnpm install
pnpm build
pnpm preview # Fails with TypeErrorimport debug from "debug";
debug.log = (...args: any[]) => {
console.log("[custom]", ...args);
};
const testNamespace = debug("test");
testNamespace("Hello from test namespace");var import_src = /* @__PURE__ */ __toESM(require_cjs_module(), 1);
import_src.log = (...args) => { ... }; // FAILS - getter-only property
(0, import_src.default)("test"); // Works - uses .defaultvar import_src = /* @__PURE__ */ __toESM(require_cjs_module(), 1);
import_src.default.log = (...args) => { ... }; // Works - accesses actual module.exports
(0, import_src.default)("test"); // Works| Bundler | Property Assignment | Function Call | Result |
|---|---|---|---|
| Rolldown | import_src.prop |
import_src.default() |
FAILS |
| esbuild | import_src.default.prop |
import_src.default() |
Works |
| Rollup | mod.prop |
mod() |
Works |