Skip to content

When entrypoint fails to resolve via ESM, fallback to CommonJS resolution #1649

Closed
@cspotcode

Description

@cspotcode

Alternative to #1611. Maybe I could tweak #1611, but I dunno, I'd prefer to track the two ideas as separate tickets.

Based on nodejs/node#41711, it seems likely that the node team is going to implement special-casing for the resolution of entrypoints.
If an entrypoint fails to resolve via the ESM loader, node will fallback to using the CommonJS resolver and setting the format to CommonJS. This handles extensionless entrypoints such as mocha's bin entrypoint.

We can do the same in our --loader, as a stop-gap until node fixes the bug on their end. resolve() invocations without parentURL can be assumed to be the entrypoint. For these requests:

  • if resolve() returns an error, try require.resolve() and return that
  • if load() / getFormat() returns an error, try returning format CommonJS

Won't be perfect, but should be good enough to unblock people while the node team figures out semantics. Once they've got that figured out, we can adjust our behavior to align, and eventually remove this hack.

Gotchas

load() does not receive parentURL, so we'll need to maintain a side-channel where we store that we have guessed a resolution is the entrypoint. Doesn't need to be perfect because this is a hack anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions