react / react/metro

Metro Resolver and Node.js Resolve Different package.json Files for the Same Package

Open
#1,517 6 comments 0 reactions 0 assignees View on GitHub

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

  1. 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
  1. Modified node_modules/msw/native/package.json to use lib2 instead of lib:
{
  "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
    }
  }
}
  1. Test in two environments:
    • Node.js environment: Uses node_modules/msw/package.json
    • Metro bundler environment: Uses node_modules/msw/native/package.json

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:

  1. Different module resolution paths
  2. Inconsistent behavior between development and production
  3. Potential runtime errors when switching between Node.js and Metro environments

Test Cases

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
    the output of node.js

  • output from metro
    the output of metro

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.