Metro Resolver and Node.js Resolve Different package.json Files for the Same Package
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 5.6k
- Forks
- 696
- Avg merge
- 8m
- Merged PRs (30d)
- 7
Description
Issue Description
There appears to be an inconsistency between how Metro bundler and Node.js resolve package.json files for the same package. This can lead to different behavior depending on the runtime environment.
Reproduction Steps
- Set up a package with nested package.json files:
node_modules/msw/
├── lib/
│ └── [original files]
├── lib2/
│ └── [copied files]
├── native/
│ └── package.json # Modified to use lib2
└── package.json # using lib
- Modified
node_modules/msw/native/package.jsonto uselib2instead oflib:
{
"type": "commonjs",
"browser": null,
"main": "../lib2/native/index.js",
"module": "../lib2/native/index.mjs",
"types": "../lib2/native/index.d.ts",
"exports": {
".": {
"react-native": {
"import": {
"types": "./../lib2/native/index.d.mts",
"default": "./../lib2/native/index.mjs"
},
"default": {
"types": "./../lib2/native/index.d.ts",
"default": "./../lib2/native/index.js"
}
}
// ... other exports
}
}
}
- Test in two environments:
- Node.js environment: Uses
node_modules/msw/package.json - Metro bundler environment: Uses
node_modules/msw/native/package.json
- Node.js environment: Uses
Expected Behavior
Both Node.js and Metro bundler should resolve the same package.json file for a given package, ensuring consistent behavior across environments.
Actual Behavior
- Node.js picks up
node_modules/msw/package.json - Metro bundler picks up
node_modules/msw/native/package.json
Impact
This inconsistency can lead to:
- Different module resolution paths
- Inconsistent behavior between development and production
- Potential runtime errors when switching between Node.js and Metro environments
Test Cases
- Node.js test case: node-msw-test
- Metro test case: my-app/metro-bundler
Environment
- Metro version: v0.82.4
- Node.js version: v22.15.0
- React Native version: v0.79.3
Additional Context
This issue was discovered while working with the MSW (Mock Service Worker) package, but it could affect any package with nested package.json files.
Screenshots
[Include the screenshots showing the different outputs from Node.js and Metro]
-
output from node.js
-
output from metro
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the discrepancy with the linked node-msw-test and my-app/metro-bundler cases, using the reported Metro v0.82.4 and Node.js v22.15.0 environments. Then trace the package.json resolution entry points used by Metro and compare them with Node.js; done means the behavior is consistent for packages with nested package.json files and the relevant tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, react-native
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100