Skip to content

[react-native] Unable to use React.lazy of packages libraries, because of "Metro has encountered an error" #22365

Closed as not planned
@whalemare

Description

@whalemare

Current Behavior

Unable to use lazy-loading React feature with react-native

This line will cause error

const SharedLayoutLazy = React.lazy(() => import('@happynrwl/shared-ui-layout'));
Error: Metro has encountered an error: Cannot resolve ./packages/shared-ui-layout/src/index: /Users/whalemare/dev/whalemare/temp/happynrwl/node_modules/@nx/react-native/plugins/metro-resolver.js

Expected Behavior

Lazy imports from packages resolved correctly

GitHub Repo

https://github.com/whalemare/nx-react-native-lazy-reproduce.git

Steps to Reproduce

  1. Setup reproducible demo
git clone https://github.com/whalemare/nx-react-native-lazy-reproduce.git
cd nx-react-native-lazy-reproduce
yarn
  1. Run android, be sure that app run without error
yarn nx run mobile:run-android
  1. Uncomment 18 line and comment 17 line in example

React-native unable to resolve imports now

Nx Report

NX   Report complete - copy this into the issue template

Node   : 18.19.0
OS     : darwin-arm64
npm    : 10.2.3

nx                 : 18.1.1
@nx/js             : 18.1.1
@nx/jest           : 18.1.1
@nx/linter         : 18.1.1
@nx/eslint         : 18.1.1
@nx/workspace      : 18.1.1
@nx/detox          : 18.1.1
@nx/devkit         : 18.1.1
@nx/eslint-plugin  : 18.1.1
@nx/react          : 18.1.1
@nx/react-native   : 18.1.1
@nrwl/tao          : 18.1.1
@nx/web            : 18.1.1
@nx/webpack        : 18.1.1
typescript         : 5.3.3

Failure Logs

Error: Metro has encountered an error: Cannot resolve ./packages/shared-ui-layout/src/index: /Users/whalemare/dev/whalemare/temp/happynrwl/node_modules/@nx/react-native/plugins/metro-resolver.js

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

Also, I apply fix from #19329 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions