microsoft / react-native-macos

A framework for building native macOS apps with React.
https://microsoft.github.io/react-native-windows/
MIT License
3.39k stars 130 forks source link

Unable to resolve module #2053

Open marandaneto opened 6 months ago

marandaneto commented 6 months ago

Environment

react-native -v: 10.2.6
npm ls react-native-macos: 0.71.36
node -v: 18.19.0
npm -v: 10.2.3
yarn --version: 1.22.21
xcodebuild -version: 15.2

Steps to reproduce the bug

Try to load a dependency using require that may or may not be installed.

import type ReactNativeNavigation from '@react-navigation/native'
import { Platform } from 'react-native'

export let OptionalReactNativeNavigation: typeof ReactNativeNavigation | undefined = undefined

try {
  if (Platform.OS !== 'macos') {
    OptionalReactNativeNavigation = require('@react-navigation/native')
  }
} catch (e) {}

This code is gonna be run on macos, so the if condition returns false, the require('@react-navigation/native') is never executed, still, RN will throw an error Unable to resolve module @react-navigation/native.

More details:

Unable to resolve module @react-navigation/native from /Users/user/Github/posthog-js-lite/examples/example_rn_macos/.yalc/posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeNavigation.js: @react-navigation/native could not be found within the project or in these directories: node_modules ../../node_modules ../../../node_modules > 1 | "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.OptionalReactNativeNavigation=void 0;var _reactNative=require("react-native");var OptionalReactNativeNavigation=undefined;exports.OptionalReactNativeNavigation=OptionalReactNativeNavigation;try{if(_reactNative.Platform.OS!=='macos'){exports.OptionalReactNativeNavigation=OptionalReactNativeNavigation=require('@react-navigation/native');}}catch(e){}   | ^

RCTFatal __28-[RCTCxxBridge handleError:]_block_invoke _dispatch_call_block_and_release _dispatch_client_callout _dispatch_main_queue_drain _dispatch_main_queue_callback_4CF CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE __CFRunLoopRun CFRunLoopRunSpecific RunCurrentEventLoopInMode ReceiveNextEventCommon _BlockUntilNextEventMatchingListInModeWithFilter _DPSNextEvent -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] -[NSApplication run] NSApplicationMain main start

If the dependency is installed but not used, everything works fine. This works well on Android/iOS/Web

Expected Behavior

Since the require('@react-navigation/native') code is never executed, it should not have thrown an error, and even if it did, there's a try-catch that should have swallowed the exception instead of throwing the error.

Actual Behavior

Unable to resolve module @react-navigation/native from /Users/user/Github/posthog-js-lite/examples/example_rn_macos/.yalc/posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeNavigation.js: @react-navigation/native could not be found within the project or in these directories: node_modules ../../node_modules ../../../node_modules > 1 | "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.OptionalReactNativeNavigation=void 0;var _reactNative=require("react-native");var OptionalReactNativeNavigation=undefined;exports.OptionalReactNativeNavigation=OptionalReactNativeNavigation;try{if(_reactNative.Platform.OS!=='macos'){exports.OptionalReactNativeNavigation=OptionalReactNativeNavigation=require('@react-navigation/native');}}catch(e){}   | ^

RCTFatal __28-[RCTCxxBridge handleError:]_block_invoke _dispatch_call_block_and_release _dispatch_client_callout _dispatch_main_queue_drain _dispatch_main_queue_callback_4CF CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE __CFRunLoopRun CFRunLoopRunSpecific RunCurrentEventLoopInMode ReceiveNextEventCommon _BlockUntilNextEventMatchingListInModeWithFilter _DPSNextEvent -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] -[NSApplication run] NSApplicationMain main start

Reproducible Demo

https://github.com/PostHog/posthog-js-lite/pull/162

Additional context

Removing import type ReactNativeNavigation from '@react-navigation/native' and making everything any also does not help. Somehow the runtime analyses all dynamically loaded libraries even if they are not requested.