Skip to content

defining Object.prototype.then causes esm loader to throw an errorΒ #45035

@KhafraDev

Description

@KhafraDev

Version

v18.11.0

Platform

Microsoft Windows NT 10.0.19043.0 x64

Subsystem

No response

What steps will reproduce the bug?

Object.prototype.then = (onFulfilled) => {
	delete Object.prototype.then
	onFulfilled({ value: undefined, done: true })
}

How often does it reproduce? Is there a required condition?

N/A

What is the expected behavior?

It would be great not to throw an error.

What do you see instead?

If you import/dynamically import something before setting Object.prototype.then, you get this error:

node:internal/modules/esm/loader:527
        .then(({ module }) => module.getNamespace());
                                     ^

TypeError: Cannot read properties of undefined (reading 'getNamespace')
    at node:internal/modules/esm/loader:527:38
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.11.0

however if you use a dynamic import after it, you get a different error:

node:internal/errors:484
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected a url string to be returned for the "url" from the "node:internal/modules/esm/resolve 'resolve'" 
function but got type undefined.
    at new NodeError (node:internal/errors:393:5)
    at ESMLoader.resolve (node:internal/modules/esm/loader:872:13)
    at async ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:7)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async file:///C:/Users/me/Documents/test.mjs:6:1 {
  code: 'ERR_INVALID_RETURN_PROPERTY_VALUE'
}

Additional information

this is a very niche use case

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions