Skip to content

fix(template): disable hierarchical lookup in Metro config to fix mod… #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jkrmarmol
Copy link

Summary

While working with a JavaScript Library module generated using create-react-native-library, I encountered module resolution issues when integrating the library into an Expo-managed project. React components and other dependencies failed to load correctly, which was caused by Metro’s default hierarchical lookup behavior.

Upon reviewing the generated metro.config.js, I found that it did not explicitly disable hierarchical lookup. This was resolved by adding the following line:

config.resolver.disableHierarchicalLookup = true;

Error Encountered Without This Fix

When disableHierarchicalLookup is not set to true, the app throws this error at runtime:

image

This error typically indicates duplicate React instances or improper module resolution, both symptoms of Metro resolving dependencies from unexpected directories due to hierarchical lookup.

Rationale

By default, Metro traverses parent directories when resolving modules, which can lead to:

  • React components not loading
  • Duplicate or conflicting dependency versions
  • Unexpected module resolution behavior in monorepo or workspace environments

This is especially problematic when using Expo or working in monorepos, as described in the Expo Monorepo Guide (Step 3). To avoid this, Expo recommends explicitly setting disableHierarchicalLookup to true to ensure Metro resolves modules only from the expected local node_modules path.

Affected File

This update applies to the metro.config.js generated when selecting:

? What type of library do you want to develop? › JavaScript Library

Testing

To verify the fix:

  1. Run the CLI command to create a new module:
    packages\create-react-native-library\bin\create-react-native-library.cmd
  2. Select JavaScript Library as the module type.
  3. After generation, open the file at example/metro.config.js.
  4. Confirm that the line
    config.resolver.disableHierarchicalLookup = true;
    has been added.

This confirms that the updated configuration is correctly included in the generated example project.

@satya164
Copy link
Member

Thanks for the PR. Can you provide more details on the exact issue you faced and share a repo that reproduces the issue?

Our config should already prevent importing multiple versions of React by blocking duplicate versions of packages defined in peer dependency. If this is not working correctly, I want to identify the bug in the blocking mechanism, and fix that. I'd prefer to keep Metro's resolution mechanism behavior similar to Node.js to avoid inconsistencies with other tools in the project.

@jkrmarmol
Copy link
Author

Thanks for the PR. Can you provide more details on the exact issue you faced and share a repo that reproduces the issue?

Our config should already prevent importing multiple versions of React by blocking duplicate versions of packages defined in peer dependency. If this is not working correctly, I want to identify the bug in the blocking mechanism, and fix that. I'd prefer to keep Metro's resolution mechanism behavior similar to Node.js to avoid inconsistencies with other tools in the project.

I’ve created a minimal reproduction repository here

Steps to reproduce the issue:

  1. Clone the repo and run the example app (yarn && yarn example start).
  2. You'll see the following issue:
    image

@satya164
Copy link
Member

Hey @jkrmarmol, I tested your repro but don't get this error.

CleanShot 2025-05-22 at 23 55 11@2x

Can you share info about your environment?

  • Node version
  • Platform you're testing on

@jkrmarmol
Copy link
Author

I'm currently using Windows 11 as my OS.

Node version: v22.13.1
Platform: Android Expo Go
OS: Windows 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants