Skip to content

[Bug] Cannot import mjs from cjs if this mjs file is already planned to be imported (ReferenceError: default is not defined) #58061

Description

@vitaliylag

Version

23.11.0

Platform

Debian

What steps will reproduce the bug?

Create module1.mjs:

import './module2.cjs';
import './module3.mjs';

Create module2.cjs:

require('./module3.mjs');

Create module3.mjs:

export default 1;

Launch:

node module1.mjs

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

Activity

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