react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

ERROR Invariant Violation: Your JavaScript code tried to access a native module that doesn't exist.

Open Randy1Burrell opened this issue 1 year ago • 34 comments

After following the instructions for installing in expo app, I get the error ERROR Invariant Violation: Your JavaScript code tried to access a native module that doesn't exist. when I try to initialize the plugin. Can someone help me to fix this?

Your Environment

  • Plugin version: "react-native-background-geolocation": "^4.16.1",
  • Platform: iOS or Android Both
  • OS version: Emulator Latest Versions
  • Device manufacturer / model: Emulators (iPhone 15 Pro Max, Pixel 7 Pro)
  • React Native version (react-native -v): Package.json
  "dependencies": {
    "expo": "~51.0.2",
    "expo-camera": "~15.0.5",
    "expo-constants": "~16.0.1",
    "expo-face-detector": "~12.7.1",
    "expo-gradle-ext-vars": "^0.1.2",
    "expo-linking": "~6.3.1",
    "expo-location": "~17.0.1",
    "expo-router": "~3.5.11",
    "expo-status-bar": "~1.12.1",
    "expo-system-ui": "~3.0.4",
    "expo-task-manager": "~11.8.1",
    "prettier": "^3.2.5",
    "react": "18.2.0",
    "react-native": "0.74.1",
    "react-native-background-fetch": "^4.2.5",
    "react-native-background-geolocation": "^4.16.1",
    "react-native-maps": "1.14.0",
    "react-native-safe-area-context": "4.10.1",
    "react-native-screens": "3.31.1",
    "zustand": "^4.5.2"
  },
  • Plugin config
  useEffect(() => {

      BackgroundGeolocation.ready({
        desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
        distanceFilter: 10,
        stopOnTerminate: false,
        startOnBoot: true,
        debug: true,
        url: 'https://localhost:3000',
        params: {
          user_id: 123,
        },
        headers: {
          'my-auth-token': 'secret-key',
        },
      }).then((state) => {
        console.log(`${state} ready`);
        BackgroundGeolocation.start();
      });
   
  });

Expected Behavior

It should work.

Actual Behavior

I get an error

Steps to Reproduce

  1. Follow the instructions for installing in expo
  2. Add the config to your functional component
  3. Run the app using npx expo start
  4. Load in your emulator or on your phone via expo go

Context

Initialize the plugin

Debug logs

Logs

ERROR Invariant Violation: Your JavaScript code tried to access a native module that doesn't exist.

If you're trying to use a module that is not supported in Expo Go, you need to create a development build of your app. See https://docs.expo.dev/development/introduction/ for more info., js engine: hermes at ContextNavigator (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:148226:24) at ExpoRoot (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:148182:28) at App at ErrorToastContainer (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:162608:24) at ErrorOverlay at withDevTools(ErrorOverlay) (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:162111:27) at RCTView at View (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:40811:43) at RCTView at View (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:40811:43) at AppContainer (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:40654:25) at main(RootComponent) (http://192.168.50.129:8081/node_modules/expo-router/entry.bundle//&platform=ios&dev=true&hot=false&lazy=true&transform.engine=hermes&transform.bytecode=true&transform.routerRoot=src%2Fapp:118984:28) ERROR Invariant Violation: Your JavaScript code tried to access a native module that doesn't exist.

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

I suggest you go back to the Setup Instructions and carefully review every line for mistakes or missed instructions.

christocracy avatar May 09 '24 22:05 christocracy

@christocracy Thanks for your quick response.

I reviewed it several times before posting this issue. Can you tell me what specifically I'm missing here?

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

Show me the url of the instructions you followed.

christocracy avatar May 09 '24 22:05 christocracy

Can you tell me what specifically I'm missing here?

Your app thinks you’ve not installed the plugin.

christocracy avatar May 09 '24 22:05 christocracy

Here you go https://github.com/transistorsoft/react-native-background-geolocation/blob/master/help/INSTALL-EXPO.md

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

I followed these instructions to the T and checked if a step was missed on several occasions. I can't say that I missed any steps.

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

So you performed the final step “Rebuild”?

christocracy avatar May 09 '24 22:05 christocracy

Yes, that local prebuild step.

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

Post your app.json

christocracy avatar May 09 '24 22:05 christocracy

Here you go:

{
  "expo": {
    "name": "appname",
    "slug": "appname",
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "userInterfaceStyle": "light",
    "scheme": "https://appname.com",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "infoPlist": {
        "NSLocationAlwaysAndWhenInUseUsageDescription": "$(PRODUCT_NAME) requires to use you location in the background",
        "NSLocationWhenInUseUsageDescription": "$(PRODUCT_NAME) requires location while in use",
        "NSMotionUsageDescription": "$(PRODUCT_NAME) uses motion-detection to determine the motion-activity of the device (walking, vehicle, bicycle, etc)",
        "UIBackgroundModes": [
          "fetch",
          "location",
          "processing",
          "remote-notification",
          "audio"
        ],
        "BGTaskSchedulerPermittedIdentifiers": [
          "com.transistorsoft.fetch",
          "com.transistorsoft.customtask"
        ]
      },
      "supportsTablet": true,
      "bundleIdentifier": "com.app.appname"
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#ffffff"
      },
      "package": "com.app.appname"
    },
    "web": {
      "bundler": "metro",
      "favicon": "./assets/favicon.png"
    },
    "plugins": [
      "expo-router",
      [
        "react-native-background-geolocation",
        {
          "license": "YOUR_LICENSE_KEY_HERE"
        }
      ],
      [
        "expo-gradle-ext-vars",
        {
          "googlePlayServicesLocationVersion": "21.1.0",
          "appCompatVersion": "1.4.2"
        }
      ],
      "react-native-background-fetch"
    ]
  }
}

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

What does the output of the command npx expo prebuild print out?

christocracy avatar May 09 '24 22:05 christocracy

The prebuild output:

zsh: correct 'expo' to '.expo' [nyae]? n
✔ Created native directories | reusing /android, /ios
✔ Updated package.json | no changes
[ expo-gradle-ext-vars] applying ext vars to android/build.gradle Map(2) {
  'googlePlayServicesLocationVersion' => '21.1.0',
  'appCompatVersion' => '1.4.2'
}
✔ Finished rebuild

Randy1Burrell avatar May 09 '24 22:05 Randy1Burrell

Post your android/build.gradle

christocracy avatar May 09 '24 22:05 christocracy

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
// @generated begin expo-gradle-ext-vars - expo prebuild (DO NOT MODIFY) sync-a14167d3df9d6380c3d77a8f8070215ea8a94013
	googlePlayServicesLocationVersion = "21.1.0"
	appCompatVersion = "1.4.2"
// @generated end expo-gradle-ext-vars
        buildToolsVersion = findProperty('android.buildToolsVersion') ?: '34.0.0'
        minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '23')
        compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '34')
        targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '34')
        kotlinVersion = findProperty('android.kotlinVersion') ?: '1.9.23'

        ndkVersion = "26.1.10909125"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath('com.android.tools.build:gradle')
        classpath('com.facebook.react:react-native-gradle-plugin')
        classpath('org.jetbrains.kotlin:kotlin-gradle-plugin')
    }
}

apply plugin: "com.facebook.react.rootproject"

allprojects {
    repositories {
// @generated begin react-native-background-fetch-maven - expo prebuild (DO NOT MODIFY) sync-b86324ce2eb77b03cc8b69ba206ef8275cd006ff
	maven { url "${project(":react-native-background-fetch").projectDir}/libs" }
// @generated begin react-native-background-geolocation-maven - expo prebuild (DO NOT MODIFY) sync-4b2bae87fd8579c445d8885f6bdc8542d9d0bbca
	maven { url "${project(":react-native-background-geolocation").projectDir}/libs" }
	maven { url 'https://developer.huawei.com/repo/' }
// @generated end react-native-background-geolocation-maven
// @generated end react-native-background-fetch-maven
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url(new File(['node', '--print', "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim(), '../android'))
        }
        maven {
            // Android JSC is installed from npm
            url(new File(['node', '--print', "require.resolve('jsc-android/package.json', { paths: [require.resolve('react-native/package.json')] })"].execute(null, rootDir).text.trim(), '../dist'))
        }

        google()
        mavenCentral()
        maven { url 'https://www.jitpack.io' }
    }
}
// @generated begin expo-camera-import - expo prebuild (DO NOT MODIFY) sync-f244f4f3d8bf7229102e8f992b525b8602c74770
def expoCameraMavenPath = new File(["node", "--print", "require.resolve('expo-camera/package.json')"].execute(null, rootDir).text.trim(), "../android/maven")
allprojects { repositories { maven { url(expoCameraMavenPath) } } }
// @generated end expo-camera-import

Randy1Burrell avatar May 09 '24 23:05 Randy1Burrell

Everything is correct.

restart your computer.

christocracy avatar May 09 '24 23:05 christocracy

Ok give me a few minutes, I'm going to do that right now.

Randy1Burrell avatar May 09 '24 23:05 Randy1Burrell

@christocracy I'm having the same issue. The restart didn't work.

Randy1Burrell avatar May 09 '24 23:05 Randy1Burrell

I have no clue. Create a fresh new hello-world app which reproduces the problem, share it with me in a public GitHub repo.

christocracy avatar May 09 '24 23:05 christocracy

Can I share the current app with you?

Randy1Burrell avatar May 09 '24 23:05 Randy1Burrell

Go ahead

christocracy avatar May 09 '24 23:05 christocracy

Second thought: no. I want you to create a simple hello-world with minimum dependencies.

christocracy avatar May 09 '24 23:05 christocracy

Ok I will do that

Randy1Burrell avatar May 09 '24 23:05 Randy1Burrell

@christocracy here you go https://github.com/Randy1Burrell/location-test-app

Randy1Burrell avatar May 09 '24 23:05 Randy1Burrell

Your app location-test-app works successfully for me. The app launches, the plugin requests permissions and enters the stationary state. I hear the debug sound FX.

christocracy avatar May 10 '24 14:05 christocracy

After cloning:

$ npm install
$ npx expo prebuild
$ npx expo run:android
.
.
.
$ npx expo run:ios

christocracy avatar May 10 '24 14:05 christocracy

Something is seriously wrong here, this is what I get whenever I run the ios command:

/Users/randyburrell/.../hello-world/hello-world/ios/Pods/Pods.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 9.0, but the range of supported deployment target versions is 12.0 to 17.4.99. (in target 'RNBackgroundGeolocation-TSLocationManagerPrivacy' from project 'Pods')
/Users/randyburrell/.../hello-world/hello-world/ios/Pods/Pods.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 12.0 to 17.4.99. (in target 'RNBackgroundFetch-TSBackgroundFetchPrivacy' from project 'Pods')
/Users/randyburrell/.../hello-world/hello-world/ios/Pods/Pods.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 11.0, but the range of supported deployment target versions is 12.0 to 17.4.99. (in target 'CocoaLumberjack-CocoaLumberjackPrivacy' from project 'Pods')
warning: Run script build phase '[CP-User] [RN]Check rncore' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'React-Fabric' from project 'Pods')


--- xcodebuild: WARNING: Using the first of multiple matching destinations:
{ platform:iOS Simulator, id:268A0CC4-A279-4F0C-AF82-2917C16DCB0B, OS:17.2, name:iPhone 15 Pro Max }
{ platform:iOS Simulator, id:268A0CC4-A279-4F0C-AF82-2917C16DCB0B, OS:17.2, name:iPhone 15 Pro Max }
** BUILD FAILED **


The following build commands failed:
	PhaseScriptExecution [CP-User]\ Generate\ Specs /Users/randyburrell/Library/Developer/Xcode/DerivedData/helloworld-dxqkzemtgskulxeohtqcdmibztys/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/React-Codegen.build/Script-46EB2E000168E0.sh (in target 'React-Codegen' from project 'Pods')
(1 failure)
Build logs written to /Users/randyburrell/.../hello-world/hello-world/.expo/xcodebuild.log

Randy1Burrell avatar May 10 '24 15:05 Randy1Burrell

The problem is on your development ecosystem. I suggest you Google your errors.

christocracy avatar May 10 '24 15:05 christocracy

These I can fix, but I am lost here as to why this is happening. I have not made any modifications to my environments at all so I would expect this to just work.

After cloning:

$ npm install
$ npx expo prebuild
$ npx expo run:android
.
.
.
$ npx expo run:ios

Based on your response above, are you saying I should not run npx expo start but instead use the commands for running on a single platform?

Randy1Burrell avatar May 10 '24 15:05 Randy1Burrell

I really don't have much day-to-day experience with Expo. I'm showing you what I did to launch the apps on my devices.

christocracy avatar May 10 '24 15:05 christocracy

Me neither, I am just testing out the expo ecosystem for an app I am developing. I might switch to Flutter since I haven't reached very far in development.

Randy1Burrell avatar May 10 '24 15:05 Randy1Burrell