fix(template): disable hierarchical lookup in Metro config to fix mod… #849
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.