-
Notifications
You must be signed in to change notification settings - Fork 218
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
base: main
Are you sure you want to change the base?
Conversation
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: |
Hey @jkrmarmol, I tested your repro but don't get this error. Can you share info about your environment?
|
I'm currently using Windows 11 as my OS. Node version: v22.13.1 |
I think this is a compatibility issue. I created a clean example using the CLI (not using Expo), and I can reproduce the same error when building on Windows. However, the build works fine on macOS. |
I was getting same
|
The problem of duplicate react instance comes from root |
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:Error Encountered Without This Fix
When disableHierarchicalLookup is not set to true, the app throws this error at runtime:
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:
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 localnode_modules
path.Affected File
This update applies to the
metro.config.js
generated when selecting:Testing
To verify the fix:
example/metro.config.js
.This confirms that the updated configuration is correctly included in the generated example project.