Fatal Exception: NSInvalidArgumentException Application tried to present modally a view controller

Error: Fatal Exception: NSInvalidArgumentException Application tried to present modally a view controller <RCTModalHostViewController: 0x10811fc50> that is already being presented by <UIViewController: 0x1055189b0>

When: When I tried to replace navigation stack using: navigation.dispatch(StackActions.replace(“CreateAccount”));

Platform: iOS only on production (not on local)

Error Details:
Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0xa248 __exceptionPreprocess
1  libobjc.A.dylib                0x17a68 objc_exception_throw
2  UIKitCore                      0x327fc0 -[UIViewController _presentViewController:withAnimationController:completion:]
3  UIKitCore                      0x326f24 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke
4  UIKitCore                      0x3051dc -[UIViewController _performCoordinatedPresentOrDismiss:animated:]
5  UIKitCore                      0xd33f8 -[UIViewController _presentViewController:animated:completion:]
6  UIKitCore                      0xd3234 -[UIViewController presentViewController:animated:completion:]
7  Luna                           0x34fc2c __76-[RCTModalHostViewManager presentModalHostView:withViewController:animated:]_block_invoke.18 + 82 (RCTModalHostViewManager.m:82)
8  libdispatch.dylib              0x24b4 _dispatch_call_block_and_release
9  libdispatch.dylib              0x3fdc _dispatch_client_callout
10 libdispatch.dylib              0x127f4 _dispatch_main_queue_drain
11 libdispatch.dylib              0x12444 _dispatch_main_queue_callback_4CF
12 CoreFoundation                 0x9aa08 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
13 CoreFoundation                 0x7c368 __CFRunLoopRun
14 CoreFoundation                 0x811e4 CFRunLoopRunSpecific
15 GraphicsServices               0x1368 GSEventRunModal
16 UIKitCore                      0x3a2d88 -[UIApplication _run]
17 UIKitCore                      0x3a29ec UIApplicationMain
18 Luna                           0x6354 main + 7 (main.m:7)
19 ???                            0x1c7811948 (Missing)

@wodin Can you please take look at it?

Hi @rshanidhi

When you say “not on local”, do you mean it works in Expo Go? Or a development build? Or what?

What do you get if you run npx expo-env-info inside the project?

What do you get if you run npx expo-doctor? And npx expo install --check?

By “not on local” I mean it works on both Expo Go and development build (simulator) but it throws the above fatal error on testflight build (on real device).
Not just replace, I have tried for reset, popToTop but all throws the same error.

My Purpose: To clear the navigation stacks to prevent user going back to screen not intended for back.
For example: after login/register

Here’s my expo-env-info:

expo-env-info 1.0.5 environment info:
    System:
      OS: macOS 13.3.1
      Shell: 5.9 - /bin/zsh
    Binaries:
      Node: 16.17.0 - /usr/local/bin/node
      Yarn: 1.22.19 - /usr/local/bin/yarn
      npm: 9.2.0 - ~/node_modules/.bin/npm
    Managers:
      CocoaPods: 1.11.3 - /usr/local/bin/pod
    SDKs:
      iOS SDK:
        Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4
    IDEs:
      Android Studio: 2021.3 AI-213.7172.25.2113.9123335
      Xcode: 14.3/14E222b - /usr/bin/xcodebuild
    npmPackages:
      @expo/webpack-config: ^18.0.1 => 18.0.1 
      expo: ^47.0.0 => 47.0.13 
      react: 18.1.0 => 18.1.0 
      react-dom: 18.1.0 => 18.1.0 
      react-native: 0.70.8 => 0.70.8 
      react-native-web: ~0.18.7 => 0.18.12 
    npmGlobalPackages:
      eas-cli: 3.10.0
      expo-cli: 6.3.2
    Expo Workflow: managed

And there was a package inconsistencies for @expo/webpack-config.
I fixed it and built new test-flight build but the error still persists.

OK. Can you post your dependencies and devDependencies from package.json? And also the plugins section from app.json if you use config plugins. (But of course you should obscure any sensitive information in the plugins props.)

Here it is:

package.json

"dependencies": {
    "@babel/runtime": "^7.20.13",
    "@expo/vector-icons": "^13.0.0",
    "@expo/webpack-config": "^0.17.2",
    "@notifee/react-native": "^7.3.0",
    "@react-native-async-storage/async-storage": "~1.17.3",
    "@react-native-community/datetimepicker": "6.5.2",
    "@react-native-community/netinfo": "9.3.5",
    "@react-native-firebase/analytics": "^16.5.0",
    "@react-native-firebase/app": "^16.5.0",
    "@react-native-firebase/crashlytics": "^16.5.0",
    "@react-native-firebase/messaging": "^16.5.0",
    "@react-navigation/bottom-tabs": "^6.3.2",
    "@react-navigation/native": "^6.0.10",
    "@react-navigation/native-stack": "^6.7.0",
    "@reduxjs/toolkit": "^1.8.6",
    "@shopify/flash-list": "1.3.1",
    "axios": "^0.27.2",
    "clamp": "^1.0.1",
    "date-fns": "^2.29.1",
    "deprecated-react-native-prop-types": "^2.3.0",
    "expo": "^47.0.0",
    "expo-apple-authentication": "~5.0.1",
    "expo-application": "~5.0.1",
    "expo-auth-session": "~3.8.0",
    "expo-build-properties": "~0.4.1",
    "expo-cellular": "~5.0.1",
    "expo-constants": "~14.0.2",
    "expo-contacts": "~11.0.1",
    "expo-dev-client": "~2.0.1",
    "expo-font": "~11.0.1",
    "expo-image-picker": "~14.0.2",
    "expo-linear-gradient": "~12.0.1",
    "expo-random": "~13.0.0",
    "expo-splash-screen": "~0.17.5",
    "expo-updates": "~0.15.6",
    "prop-types": "^15.8.1",
    "react": "18.1.0",
    "react-dom": "18.1.0",
    "react-hook-form": "^7.34.0",
    "react-native": "0.70.8",
    "react-native-confirmation-code-field": "^7.3.0",
    "react-native-exception-handler": "^2.10.10",
    "react-native-fast-image": "^8.6.3",
    "react-native-linear-gradient": "^2.6.2",
    "react-native-pager-view": "6.0.1",
    "react-native-plaid-link-sdk": "^8.0.0",
    "react-native-reanimated": "~2.12.0",
    "react-native-safe-area-context": "4.4.1",
    "react-native-screens": "~3.18.0",
    "react-native-shadow-2": "^7.0.6",
    "react-native-shimmer-placeholder": "^2.0.9",
    "react-native-svg": "13.4.0",
    "react-native-tab-view": "^3.3.4",
    "react-native-typography": "^1.4.1",
    "react-native-web": "~0.18.7",
    "react-redux": "^8.0.2",
    "redux": "^4.2.0",
    "redux-thunk": "^2.4.1",
    "rn-sliding-up-panel": "^2.4.6"
  },
  "devDependencies": {
    "@babel/core": "^7.19.3",
    "@babel/preset-env": "^7.1.6",
    "@types/react": "~18.0.24",
    "@types/react-native": "~0.70.6",
    "@typescript-eslint/eslint-plugin": "^5.31.0",
    "@typescript-eslint/parser": "^5.40.1",
    "babel-eslint": "^10.1.0",
    "eslint": "^8.21.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "8.5.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-jsx-a11y": "^6.6.1",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "eslint-plugin-react-native": "4.0.0",
    "prettier": "2.7.1",
    "typescript": "^4.6.3"
  },

app.json

{
  "expo": {
    "owner": "owner",
    "name": "App",
    "slug": "my-slug",
    "scheme": "my-scheme",
    "version": "2.2.1",
    "orientation": "portrait",
    "icon": "./assets/app-logo.png",
    "userInterfaceStyle": "light",
    "jsEngine": "hermes",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "cover",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "url": "valid-update-url"
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.app",
      "usesAppleSignIn": true,
      "associatedDomains": [
        "applinks:myhost"
      ],
      "buildNumber": "1",
      "config": {
        "usesNonExemptEncryption": false
      },
      "infoPlist": {
        "UIViewControllerBasedStatusBarAppearance": false,
        "CFBundleAllowMixedLocalizations": true
      }
    },
    "android": {
      "package": "com.app",
      "versionCode": 1,
      "intentFilters": [
        {
          "action": "VIEW",
          "autoVerify": true,
          "data": [
            {
              "scheme": "https",
              "host": "myhost",
              "pathPrefix": "/callback"
            }
          ],
          "category": [
            "BROWSABLE",
            "DEFAULT"
          ]
        }
      ],
      "blockedPermissions": [
        "android.permission.RECORD_AUDIO",
        "android.permission.WRITE_CONTACTS"
      ],
      "adaptiveIcon": {
        "foregroundImage": "./assets/app-logo.png",
        "backgroundColor": "#FFFFFF"
      }
    },
    "web": {
      "favicon": "./assets/app-logo.png"
    },
    "plugins": [
      "@react-native-firebase/app",
      "@react-native-firebase/crashlytics",
      "expo-apple-authentication",
      "expo-image-picker",
      "@notifee/react-native",
      [
        "./withAndroidVerifiedLinksWorkaround",
        {}
      ],
      [
        "./withAndroidPlaid",
        {}
      ],
      [
        "expo-build-properties",
        {
          "android": {
            "enableProguardInReleaseBuilds": true,
            "compileSdkVersion": 33,
            "targetSdkVersion": 33,
            "extraProguardRules": "-keep class com.facebook.hermes.unicode.** { *; }\n-keep class com.facebook.jni.** { *; }\n-keep public class com.dylanvann.fastimage.* {*;}\n-keep public class com.dylanvann.fastimage.** {*;}\n-keep public class * implements com.bumptech.glide.module.GlideModule\n-keep public class * extends com.bumptech.glide.module.AppGlideModule\n-keep public enum com.bumptech.glide.load.ImageHeaderParser$** { **[] $VALUES;public *;}"
          },
          "ios": {
            "useFrameworks": "static"
          }
        }
      ]
    ],
    "runtimeVersion": {
      "policy": "sdkVersion"
    }
  }
}

I added your dependencies and plugins to a test app.

I had to remove the two following plugins because you didn’t post them, but in theory they should not affect the iOS build since they look like they are Android-specific:

I then tried to build it. It failed because I did not have a GoogleService-Info.plist file. So I generated one and added it. After that it built without a problem.

I know expo-env-info says “Expo Workflow: managed”, but can you confirm that there is no ios directory in the root of your project?

@wodin
Thank you very much for your effort.

Exactly it is expo managed project, so no ios or android dir.

OK, then maybe try the following:

  • Remove your node_modules and package-lock.json or yarn.lock.
  • Reinstall your dependencies with npm install or yarn (depending on which one you normally use).
  • Run npx expo install --check

See if that helps.