Resolver: Read `exports` from the package root, not the closest `package.json`
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 22/100
- Type d'issue
- Fonctionnalité
- Clarté
- Plutôt claire
- Activité
- Active
- Stack technique
- javascript, react-native
- Domaine
- build-system
Piste de recherche
Start in packages/metro-resolver/src/resolve.js around the line where the package is looked up once (about L420), and trace how resolveFromNodeModulesPath, extraNodeModules and Haste pass the package root down. The change adds an opt-in resolver.unstable_resolveExportsFromPackageRoot flag that reads "exports" via getPackageForModule(packageRoot) when the subpath is empty. Done means the flag gates the new behaviour, existing tests pass with it off, and new tests cover nested package.json with root exports.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Metro reads "exports" from the package.json closest to the requested subpath, not the one at the package root. So a nested {"type": "commonjs"} or a legacy shim package.json hides the root's "exports", which is different to Node's PACKAGE_RESOLVE, webpack and Vite. Previously reported in #1517 and #1582, this is to track the fix.
Examples I've seen in real apps (AI-driven comparison): @floating-ui/utils/dom on Bluesky web gets the legacy shim's build instead of dist/floating-ui.utils.dom.mjs, and on Mattermost, ESM imports of rxjs/operators get dist/cjs while rxjs gets dist/esm5. @kitten and @Nezz have seen the same thing break swr.
The fix should be small now that #2134 looks the package up once, here:
Metro converts specifiers to absolute candidate paths early, so the caller has to pass the package root down. resolveFromNodeModulesPath has parsedSpecifier.packageName, extraNodeModules has the mapped directory and Haste has the directory of its package.json.
- For
"exports", usegetPackageForModule(packageRoot). It gives a realrootPath, so pnpm symlinks work, andpackageRelativePathcomes from the specifier's subpath. - If
packageRelativePathisn't empty, there's nopackage.jsonat the root, so keep using the closest to the target. "browser"redirections,"imports"and self-references all keep using the closestpackage.json, which is correct for those.
This should go behind resolver.unstable_resolveExportsFromPackageRoot, off by default, so that we can get it out in 0.87.x without changing what anyone resolves, and then enable it by default for 0.88.
It's breaking because some packages only work on native through a nested "browser" field that this bypasses. As @Om-singhaI found, @mswjs/interceptors/fetch goes from its browser build to its Node build, which needs node:zlib. nanoid/async (nanoid 3) also moves, and needs unstable_applyBrowserSpecAfterExports on as well to avoid importing crypto. Subpaths with a nested package.json that aren't in the root's "exports" will now warn, then fall back as they do today.
- Langage dominant
- JavaScript
- Étoiles
- 5.7k
- Forks
- 715
- Merge moyen
- 3 j 14 h
- PR mergées (30 j)
- 77
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de react/metro
-
[metro-file-map] An error a watcher backend reports ends the server, since nothing listens for itPeut-être pris @sorinc03 l’a pris il y a 3 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 4/5 3-5 jours Accessibilité débutants 42/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
Les mainteneurs répondent en général sous 1 jour
-
metro-file-map gives up on a fallback watcher that is still starting after a fixed 240 s, and leaves it runningPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 48/100
Les mainteneurs répondent en général sous 1 jour
-
Published declarations import types from connect, @babel/core and @babel/traverse, whose @types packages are not dependenciesPeut-être pris @sorinc03 l’a pris il y a 3 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de react/metro
Issues similaires
-
502 on Node 26: ERR_INVALID_ARG_VALUE: Setting the TLS ServerName to an IP address is not permittedOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
github/paste-markdown#129 · 2 réactions ·
-
bug
Difficulté 1/5 1-3 heures Accessibilité débutants 83/100
obsidianmd/obsidian-help#1164 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
richardcase/skills#26 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
Les mainteneurs répondent en général sous 1 jour