Skip to content
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

Unable to resolve module #2053

Open
marandaneto opened this issue Jan 24, 2024 · 0 comments
Open

Unable to resolve module #2053

marandaneto opened this issue Jan 24, 2024 · 0 comments
Labels
bug Something isn't working Needs: Triage 🔍

Comments

@marandaneto
Copy link

marandaneto commented Jan 24, 2024

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
�[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

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

PostHog/posthog-js-lite#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Needs: Triage 🔍
Projects
None yet
Development

No branches or pull requests

1 participant