You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Cannot import mjs from cjs if this mjs file is already planned to be imported (ReferenceError: default is not defined) #58061
How often does it reproduce? Is there a required condition?
100%, no.
What is the expected behavior?
Successful launch
What do you see instead?
require('./module3.mjs') throws this error:
node:internal/modules/cjs/loader:1609
} else if (!ObjectHasOwn(namespace, 'default') || ObjectHasOwn(namespace, '__esModule')) {
^
ReferenceError: default is not defined
at Module.hasOwn (<anonymous>)
at hasOwn (<anonymous>)
at loadESMFromCJS (node:internal/modules/cjs/loader:1609:17)
at Module._compile (node:internal/modules/cjs/loader:1716:5)
at Object..js (node:internal/modules/cjs/loader:1899:10)
at Module.load (node:internal/modules/cjs/loader:1469:32)
at Function._load (node:internal/modules/cjs/loader:1286:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
at Module.require (node:internal/modules/cjs/loader:1491:12)
Node.js v23.11.0
changed the title [-]Cannot import mjs from cjs if this mjs file is already planned to be imported (ReferenceError: default is not defined)[/-][+][Bug] Cannot import mjs from cjs if this mjs file is already planned to be imported (ReferenceError: default is not defined)[/+]on Apr 28, 2025
In v22.14.0 (output for v23.9.0 is different but still gives ERR_REQUIRE_CYCLE_MODULE)
node:internal/modules/esm/loader:346
throw new ERR_REQUIRE_CYCLE_MODULE(message);
^
Error [ERR_REQUIRE_CYCLE_MODULE]: Cannot require() ES Module **PATH_TO**module3.cjs in a cycle. (from **PATH_TO**module2.cjs)
at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:346:15)
at loadESMFromCJS (node:internal/modules/cjs/loader:1385:24)
at Module._compile (node:internal/modules/cjs/loader:1536:5)
at Object..js (node:internal/modules/cjs/loader:1706:10)
at Module.load (node:internal/modules/cjs/loader:1289:32)
at Function._load (node:internal/modules/cjs/loader:1108:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
at Module.require (node:internal/modules/cjs/loader:1311:12)
at require (node:internal/modules/helpers:136:16) {
code: 'ERR_REQUIRE_CYCLE_MODULE'
}
Node.js v22.14.0
Is this the expected behavior in v22.15.0+ / v23.10.0+ ?
Activity
[-]Cannot import mjs from cjs if this mjs file is already planned to be imported (ReferenceError: default is not defined)[/-][+][Bug] Cannot import mjs from cjs if this mjs file is already planned to be imported (ReferenceError: default is not defined)[/+]alexsch01 commentedon Apr 28, 2025
First shows up in v23.10.0 and v22.15.0
ljharb commentedon Apr 28, 2025
cc @joyeecheung @nodejs/loaders
alexsch01 commentedon Apr 28, 2025
In v22.14.0 (output for v23.9.0 is different but still gives ERR_REQUIRE_CYCLE_MODULE)
node:internal/modules/esm/loader:346 throw new ERR_REQUIRE_CYCLE_MODULE(message); ^ Error [ERR_REQUIRE_CYCLE_MODULE]: Cannot require() ES Module **PATH_TO**module3.cjs in a cycle. (from **PATH_TO**module2.cjs) at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:346:15) at loadESMFromCJS (node:internal/modules/cjs/loader:1385:24) at Module._compile (node:internal/modules/cjs/loader:1536:5) at Object..js (node:internal/modules/cjs/loader:1706:10) at Module.load (node:internal/modules/cjs/loader:1289:32) at Function._load (node:internal/modules/cjs/loader:1108:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:220:24) at Module.require (node:internal/modules/cjs/loader:1311:12) at require (node:internal/modules/helpers:136:16) { code: 'ERR_REQUIRE_CYCLE_MODULE' } Node.js v22.14.0Is this the expected behavior in v22.15.0+ / v23.10.0+ ?
vitaliylag commentedon Apr 28, 2025
I'm not sure ERR_REQUIRE_CYCLE_MODULE error is expected behaivor either.
Also it works if module3 does not have default export.
joyeecheung commentedon Apr 29, 2025
Thanks for the report, fix in #58067
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)
module: handle instantiated async module jobs in require(esm)