Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNPermissionsModule' couldn't be found

503 views Asked by At

I am trying to run the code but this error keeps on showing up:

ERROR Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNPermissionsModule' could not be found. Verify that a module by this name is registered in the native binary., js engine: hermes

ERROR Invariant Violation: "main" 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: hermes

This error appears after I run the following code:

"dependencies": {
  "@react-native-community/masked-view": "^0.1.11",
  "@react-navigation/drawer": "^6.6.2",
  "@react-navigation/native": "^6.1.6",
  "expo": "~48.0.18",
  "expo-status-bar": "~1.4.4",
  "react": "18.2.0",
  "react-native": "0.71.8",
  "react-native-check-app-install": "^0.0.5",
  "react-native-exit-app": "^1.1.0",
  "react-native-gesture-handler": "~2.9.0",
  "react-native-permissions": "^3.8.0",
  "react-native-reanimated": "~2.14.4",
  "react-native-safe-area-context": "4.5.0",
  "react-native-screens": "~3.20.0"
},
"devDependencies": {
  "@babel/core": "^7.20.0"
},
"private": true

This is the main body of the code:

import React, { useEffect, useState } from 'react';
import { View, Text, Button, FlatList, Switch, TextInput, TouchableOpacity, Alert } from 'react-native';
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';
import ExitApp from 'react-native-exit-app';
import { NavigationContainer } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';

const Drawer = createDrawerNavigator();

const App = () => {
  const [installedApps, setInstalledApps] = useState([]);
  const [shortlistedApps, setShortlistedApps] = useState([]);
  const [timers, setTimers] = useState({});
  const [blockedApps, setBlockedApps] = useState([]);
  const [openedApp, setOpenedApp] = useState(null);

  useEffect(() => {
    fetchInstalledApps();
  }, []);

  const fetchInstalledApps = async () => {
    const permissionStatus = await check(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE);

    if (permissionStatus !== RESULTS.GRANTED) {
      Alert.alert(
        'Permission Required',
        'Please grant storage permission to fetch the installed apps.',
        [{ text: 'OK' }]
      );
      return;
    }

    try {
      const apps = await RNInstalledApps.getApps();
      const appNames = apps.map((app) => app.appName);
      setInstalledApps(appNames);
    } catch (error) {
      console.error('Error fetching installed apps:', error);
    }
  };

  const handleAppSelection = (app) => {
    const isShortlisted = shortlistedApps.includes(app);

    if (isShortlisted) {
      const updatedApps = shortlistedApps.filter((item) => item !== app);
      setShortlistedApps(updatedApps);
    } else {
      const updatedApps = [...shortlistedApps, app];
      setShortlistedApps(updatedApps);
    }
  };

  const handleTimeLimitChange = (app, timeLimit) => {
    const updatedTimers = { ...timers, [app]: timeLimit };
    setTimers(updatedTimers);
  };

  const handleTimerEnable = (app, enabled) => {
    if (enabled) {
      const updatedTimers = { ...timers, [app]: 0 };
      setTimers(updatedTimers);
    } else {
      const { [app]: _, ...updatedTimers } = timers;
      setTimers(updatedTimers);
    }
  };

  const openApp = (app) => {
    const timer = setInterval(() => {
      if (timers[app] > 0) {
        setTimers((prevTimers) => ({ ...prevTimers, [app]: prevTimers[app] - 1 }));
      } else {
        clearInterval(timer);
        setOpenedApp(app);
      }
    }, 1000);
  };

  const handleAppClose = () => {
    setOpenedApp(null);
    ExitApp.exitApp();
  };

  const renderAppItem = ({ item }) => {
    const timeLimit = timers[item] || 0;

    const isBlocked = blockedApps.some(
      (blockedApp) => blockedApp.app === item && blockedApp.day === new Date().getDate()
    );

    const isTimerEnabled = timeLimit > 0;
    const isAppOpened = openedApp === item;

    return (
      <View>
        <Text>{item}</Text>
        <Text>Time Used: {timeLimit}</Text>
        {isBlocked ? (
          <Text>This app is blocked for today.</Text>
        ) : (
          <>
            <Switch
              value={isTimerEnabled}
              onValueChange={(enabled) => handleTimerEnable(item, enabled)}
            />
            <TextInput
              keyboardType="numeric"
              value={timeLimit.toString()}
              onChangeText={(text) => handleTimeLimitChange(item, parseInt(text, 10))}
            />
            <TouchableOpacity onPress={() => handleAppSelection(item)}>
              <Text>{shortlistedApps.includes(item) ? 'Remove' : 'Add'}</Text>
            </TouchableOpacity>
            {!isTimerEnabled && (
              <TouchableOpacity onPress={() => openApp(item)}>
                <Text>{isAppOpened ? 'Close App' : 'Open App'}</Text>
              </TouchableOpacity>
            )}
          </>
        )}
      </View>
    );
  };

  return (
    <NavigationContainer>
      <Drawer.Navigator>
        <Drawer.Screen name="Home">
          {() => (
            <View>
              <Text>Installed Apps:</Text>
              <FlatList
                data={installedApps}
                renderItem={renderAppItem}
                keyExtractor={(item) => item}
              />
            </View>
          )}
        </Drawer.Screen>
        <Drawer.Screen name="Blocked Apps">
          {() => (
            <View>
              <Text>Blocked Apps:</Text>
              <FlatList
                data={blockedApps}
                renderItem={({ item }) => (
                  <Text>{`${item.app} - Blocked for Day ${item.day}`}</Text>
                )}
                keyExtractor={(item, index) => `${item.app}-${item.day}-${index}`}
              />
            </View>
          )}
        </Drawer.Screen>
      </Drawer.Navigator>
    </NavigationContainer>
  );
};

export default App;

I am not really sure where the problem is occurring.

1

There are 1 answers

0
Ugur On

I had the same issue when i try to run my project with npx expo start. Native modules don't work with expo. Try starting the app with "npm run android" or "npm run ios".