Skip to content

Commit 38998d8

Browse files
committed
module: do not warn when accessing __esModule of unfinished exports
Since this property access is performed by generated code, and not used for accessing the actual exports of a module (and because transpilers generally define it as the first key of `module.exports` when it *is* present), it should be okay to allow it. Refs: #29935 Fixes: #33046 PR-URL: #33048 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
1 parent b2768ae commit 38998d8

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

lib/internal/modules/cjs/loader.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,16 @@ function emitCircularRequireWarning(prop) {
824824
// warns when non-existend properties are accessed.
825825
const CircularRequirePrototypeWarningProxy = new Proxy({}, {
826826
get(target, prop) {
827-
if (prop in target) return target[prop];
827+
// Allow __esModule access in any case because it is used in the output
828+
// of transpiled code to determine whether something comes from an
829+
// ES module, and is not used as a regular key of `module.exports`.
830+
if (prop in target || prop === '__esModule') return target[prop];
828831
emitCircularRequireWarning(prop);
829832
return undefined;
830833
},
831834

832835
getOwnPropertyDescriptor(target, prop) {
833-
if (ObjectPrototypeHasOwnProperty(target, prop))
836+
if (ObjectPrototypeHasOwnProperty(target, prop) || prop === '__esModule')
834837
return ObjectGetOwnPropertyDescriptor(target, prop);
835838
emitCircularRequireWarning(prop);
836839
return undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./warning-esm-half-transpiled-b.js');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const a = require('./warning-esm-half-transpiled-a.js');
2+
a.__esModule;

test/parallel/test-module-circular-dependency-warning.js

+7
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ assert.strictEqual(Object.getPrototypeOf(classExport).name, 'Parent');
3131
const esmTranspiledExport =
3232
require(fixtures.path('cycles', 'warning-esm-transpiled-a.js'));
3333
assert.strictEqual(esmTranspiledExport.__esModule, true);
34+
35+
// If module.exports.__esModule is being accessed but is not present, e.g.
36+
// because only the one of the files is a transpiled ES module, no warning
37+
// should be emitted.
38+
const halfTranspiledExport =
39+
require(fixtures.path('cycles', 'warning-esm-half-transpiled-a.js'));
40+
assert.strictEqual(halfTranspiledExport.__esModule, undefined);

0 commit comments

Comments
 (0)