Skip to content

commonjs: Object.defineProperty(exports, "__esModule", …) leaves unbound exports when module is wrapped #2041

Closed

Description

@trackoor

Expected Behavior

When a CommonJS module's only reference to exports is a top-level Object.defineProperty(exports, "__esModule", { value: true }) and the module also references module, m.js should either receive (module, exports) in the wrapper or have exports rewritten, matching Node's CommonJS wrapper where both module and exports are in scope.

Actual Behavior

The module is wrapped in (function (module) { ... }()) without passing exports or rewriting the defineProperty call. Running the generated ES bundle throws ReferenceError: exports is not defined in ES module scope because requireM emits a free exports identifier inside the wrapper:

(function (module) {
	Object.defineProperty(exports, "__esModule", { value: true });
	console.log('object'); 
} ());

Additional Information

// m.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log(typeof module.exports);

// entry.js
require("./m.js");

// build.mjs
import { rollup } from "rollup";
import commonjs from "@rollup/plugin-commonjs";

const bundle = await rollup({ input: "entry.js", plugins: [commonjs()] });
const { output } = await bundle.generate({ format: "es" });
console.log(output[0].code);

The CallExpression handler for __esModule skips exports without marking uses.exports = true, so wrapping triggered by uses.module omits exports from the wrapper parameters.

Credit: https://ucsbplse.github.io/

Activity

  1. locked and limited conversation to collaborators on Nov 7, 2026
  2. converted this issue into a discussion #2044 on Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions