Skip to content

@rollup/plugin-commonjs: circular require receives undefined instead of {} before module.exports = #2042

Closed

Description

@trackoor

Expected Behavior

When a CommonJS module with a top-level module.exports = ... assignment is required back in a circular dependency before that assignment executes, require() should return the module's initial {} exports object, matching Node's CommonJS semantics (printing { seenByB: 'object' } in the reproduction below). Similarly, top-level this before module.exports = ... should refer to that initial {} object.

Actual Behavior

A module with a top-level module.exports = ... assignment and no other module or exports references leaves its exports variable uninitialized (var a;) until the assignment executes. If another module requires it back in a cycle before that assignment runs, require() returns undefined instead of {} (printing { seenByB: 'undefined' } in the reproduction below).

The same uninitialized var also causes top-level this accesses before module.exports = ... (e.g. this.x = 1; module.exports = { y: 2 };) to throw TypeError: Cannot set properties of undefined.

Additional Information

// a.js
const b = require('./b.js');
module.exports = { seenByB: b.seen };

// b.js
const a = require('./a.js');
module.exports = { seen: typeof a };

// entry.js
console.log(require('./a.js'));

Bundling entry.js with @rollup/plugin-commonjs (default strictRequires: true or 'auto') and running the output prints { seenByB: 'undefined' }, whereas running node entry.js directly prints { seenByB: 'object' }.

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

Activity

  1. locked and limited conversation to collaborators on Nov 7, 2026
  2. converted this issue into a discussion #2043 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