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
[0m[31m[1m>[22m[39m[90m 1 |[39m [32m"use strict"[39m[33m;[39m[33mObject[39m[33m.[39mdefineProperty(exports[33m,[39m[32m"__esModule"[39m[33m,[39m{value[33m:[39m[36mtrue[39m})[33m;[39mexports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[36mvoid[39m [35m0[39m[33m;[39m[36mvar[39m _reactNative[33m=[39mrequire([32m"react-native"[39m)[33m;[39m[36mvar[39m [33mOptionalReactNativeNavigation[39m[33m=[39mundefined[33m;[39mexports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[33mOptionalReactNativeNavigation[39m[33m;[39m[36mtry[39m{[36mif[39m(_reactNative[33m.[39m[33mPlatform[39m[33m.[39m[33mOS[39m[33m!==[39m[32m'macos'[39m){exports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[33mOptionalReactNativeNavigation[39m[33m=[39mrequire([32m'@react-navigation/native'[39m)[33m;[39m}}[36mcatch[39m(e){}[0m
[0m [90m |[39m [31m[1m^[22m[39m[0m
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
[0m[31m[1m>[22m[39m[90m 1 |[39m [32m"use strict"[39m[33m;[39m[33mObject[39m[33m.[39mdefineProperty(exports[33m,[39m[32m"__esModule"[39m[33m,[39m{value[33m:[39m[36mtrue[39m})[33m;[39mexports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[36mvoid[39m [35m0[39m[33m;[39m[36mvar[39m _reactNative[33m=[39mrequire([32m"react-native"[39m)[33m;[39m[36mvar[39m [33mOptionalReactNativeNavigation[39m[33m=[39mundefined[33m;[39mexports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[33mOptionalReactNativeNavigation[39m[33m;[39m[36mtry[39m{[36mif[39m(_reactNative[33m.[39m[33mPlatform[39m[33m.[39m[33mOS[39m[33m!==[39m[32m'macos'[39m){exports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[33mOptionalReactNativeNavigation[39m[33m=[39mrequire([32m'@react-navigation/native'[39m)[33m;[39m}}[36mcatch[39m(e){}[0m
[0m [90m |[39m [31m[1m^[22m[39m[0m
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.
Environment
Steps to reproduce the bug
Try to load a dependency using
require
that may or may not be installed.This code is gonna be run on
macos
, so theif
condition returns false, therequire('@react-navigation/native')
is never executed, still, RN will throw an errorUnable to resolve module @react-navigation/native
.More details:
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 [0m[31m[1m>[22m[39m[90m 1 |[39m [32m"use strict"[39m[33m;[39m[33mObject[39m[33m.[39mdefineProperty(exports[33m,[39m[32m"__esModule"[39m[33m,[39m{value[33m:[39m[36mtrue[39m})[33m;[39mexports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[36mvoid[39m [35m0[39m[33m;[39m[36mvar[39m _reactNative[33m=[39mrequire([32m"react-native"[39m)[33m;[39m[36mvar[39m [33mOptionalReactNativeNavigation[39m[33m=[39mundefined[33m;[39mexports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[33mOptionalReactNativeNavigation[39m[33m;[39m[36mtry[39m{[36mif[39m(_reactNative[33m.[39m[33mPlatform[39m[33m.[39m[33mOS[39m[33m!==[39m[32m'macos'[39m){exports[33m.[39m[33mOptionalReactNativeNavigation[39m[33m=[39m[33mOptionalReactNativeNavigation[39m[33m=[39mrequire([32m'@react-navigation/native'[39m)[33m;[39m}}[36mcatch[39m(e){}[0m [0m [90m |[39m [31m[1m^[22m[39m[0m
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 everythingany
also does not help. Somehow the runtime analyses all dynamically loaded libraries even if they are not requested.