Skip to content

fix(metro-resolver): Skip bare ext file resolution on ESM module resolution#1673

Open
kitten wants to merge 1 commit into
facebook:mainfrom
kitten:@kitten/experiment/skip-esm-bare-file-resolution
Open

fix(metro-resolver): Skip bare ext file resolution on ESM module resolution#1673
kitten wants to merge 1 commit into
facebook:mainfrom
kitten:@kitten/experiment/skip-esm-bare-file-resolution

Conversation

@kitten
Copy link
Copy Markdown
Contributor

@kitten kitten commented Apr 1, 2026

Summary

Note

Opening this mostly to ask whether this is intended behaviour, so it's potentially not as polished/finalized as other PRs. Marking from draft as open to run tests though

In CommonJS, it's normal for metro-resolver to probe node_modules/[moduleName][ext] for implementations. For example, for invariant, we'd probe node_modules/invariant.js, node_modules/invariant.web.js, etc first, when falling back to file resolution.

However, when resolving for an ESM import, this is unwanted, out-of-spec and redundant, and we can skip this resolution, since ESM expects a full node_modules structure rather than the legacy behaviour.

While this typically doesn't matter since files in node_modules have become increasingly rare, it can be used to skip redundant file resolution for ESM in node_modules.

The legacy behaviour still has to apply to sub-paths, since sub-path imports are otherwise also disabled for sub-paths without exports conditions that replace them. However, for the root path case, which is the most common, the added lookups look redundant to me.

Arguably, if we want the sub-path extensions behaviour to still apply to ESM we could disable file resolution entirely when package.json:exports are present, and instead rely on conditions. However, this would be a "more" breaking change.

Changelog: [Fix] Prevent direct file resolution in module paths for ESM

Test plan

Unit tests (LLM-written) have been added to demonstrate the behaviour

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 1, 2026
@kitten kitten changed the title resolver: Skip bare ext file resolution on ESM module resolution fix(metro-resolver): Skip bare ext file resolution on ESM module resolution Apr 1, 2026
@kitten kitten force-pushed the @kitten/experiment/skip-esm-bare-file-resolution branch from 8fd8509 to 4e8f2e6 Compare April 11, 2026 00:54
@kitten kitten marked this pull request as ready for review April 11, 2026 00:54
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 11, 2026
@kitten kitten force-pushed the @kitten/experiment/skip-esm-bare-file-resolution branch from 4e8f2e6 to 1c69f23 Compare April 11, 2026 13:00
@kitten kitten force-pushed the @kitten/experiment/skip-esm-bare-file-resolution branch from 1c69f23 to 59ae99e Compare April 22, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant