Search code examples
androidreact-nativemetro-bundlerreact-native-vector-icons

Native module RNVectorIcons tried to override RNVectorIcons


After following all the installation process of React Native Vector Icons on GitHub, i ended up getting this error telling me that:

Native module RNVectorIcons tries to override RNVectorIcons.  Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExsistingModule=true. 

This error may also be present if the package is present only once in getPackages() but is also automatically added later during build time by autolinking. Try removing the existing entry and rebuild.

My Application.kt looks like this:

package com.project

import android.app.Application
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import com.oblador.vectoricons.VectorIconsPackage;

class MainApplication : Application(), ReactApplication {

  override val reactNativeHost: ReactNativeHost =
      object : DefaultReactNativeHost(this) {
        override fun getPackages(): List<ReactPackage> =
            PackageList(this).packages.apply {
              add(VectorIconsPackage())
              // Packages that cannot be autolinked yet can be added manually here, for example:
              // add(MyReactNativePackage())
            }

        override fun getJSMainModuleName(): String = "index"

        override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG

        override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
        override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
      }

  override val reactHost: ReactHost
    get() = getDefaultReactHost(applicationContext, reactNativeHost)

  override fun onCreate() {
    super.onCreate()
    SoLoader.init(this, false)
    if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
      // If you opted-in for the New Architecture, we load the native entry point for this app.
      load()
    }
  }
}

If i try to remove add(VectorIconsPackage()) , i get two errors. The first error looks like this:

 ERROR  Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'VectorIcons' could not be found. Verify that a module by this name is registered in the native binary.Bridgeless mode: false. TurboModule interop: false. Modules loaded: {"NativeModules":["PlatformConstants","LogBox","Timing","AppState","SourceCode","BlobModule","WebSocketModule","DevSettings","DevToolsSettingsManager","Networking","Appearance","DevLoadingView","HeadlessJsTaskSupport","UIManager","DeviceInfo","ImageLoader","SoundManager","IntentAndroid","DeviceEventManager"],"TurboModules":[],"NotFound":["NativePerformanceCxx","NativePerformanceObserverCxx","RedBox","BugReporting","LinkingManager","VectorIcons"]}, js engine: hermes
 LOG  Running "Project" with {"rootTag":11}

The second error looks like this:


Invariant Violation: "Project" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called., js engine: Project

Dependencies:


"dependencies": {
    "@react-native-vector-icons/ionicons": "^7.4.0-alpha.16",
    "@types/react-native-vector-icons": "^6.4.18",
    "react": "18.2.0",
    "react-native": "0.74.3",
    "react-native-vector-icons": "^10.1.0"
  }

I have tried to edit fonts.gradle as per This suggestion on Github by author and yet an error persist.

Updating or reinstalling React Native Vector Icons seems not to help at all.


Solution

  • (Solved), looking at dependencies, I accidentally installed two dependencies which all use VectorIconsPackage instead of using one. I had to remove either @react-native-vector-icons/Ionicons.