Skip to content

ES module loading inconsistent with browser while handling undefined exceptions #19281

Closed
@zhanzhenzhen

Description

@zhanzhenzhen
  • Version: 9.8.0
  • Platform:
  • Subsystem:

If a module throws undefined, and another module imports it, it should catch undefined, like that in browser.

Browser Test

Make 2 files "index.html" and "aaa.mjs".

index.html:

<!DOCTYPE html>
<html>
<head>
<script type="module">
  "use strict";
  import('./aaa.mjs')
  .then(m => console.log("ok"))
  .catch(e => console.log("bad", e));
</script>
<title>test</title>
</head>
<body>
</body>
</html>

aaa.mjs:

"use strict";

throw undefined;

It will output "bad undefined". You can also visit my temporary page for this test: https://zzz-shadow.github.io/

Node.js Test

Make 2 files "test.mjs" and "aaa.mjs".

test.mjs:

"use strict";
import('./aaa.mjs')
.then(m => console.log("ok"))
.catch(e => console.log("bad", e));

aaa.mjs:

"use strict";

throw undefined;

When running test.mjs, it will output:

bad TypeError: Cannot read property 'stack' of undefined
    at ModuleJob.run (internal/loader/ModuleJob.js:108:9)

So, in Node, when a module throws undefined or null, the thing caught isn't the thing it throws.

Static imports are also affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions