Error: Cannot find native module 'ExpoBarCodeScanner'

This is my code and I am not sure why I am getting that error and installed it from npm if that may be causing the issue



import { StyleSheet, Text, View, Button, Linking } from 'react-native'
import React from 'react'
import { BarCodeScanner } from 'expo-barcode-scanner'
import { useState, useEffect } from 'react'


export default function Scanner() {
    const [hasPermission, setHasPermission] = useState(null);
    const [scanned, setScanned] = useState(false);


    useEffect(() => {
        const getBarCodeScannerPermissions = async () => {
          const { status } = await BarCodeScanner.requestPermissionsAsync();
          setHasPermission(status === 'granted');
        };
    
        getBarCodeScannerPermissions();
      }, []);

      
      const handleBarCodeScanned = ({ type, data }) => {
        setScanned(true);
        alert(`Bar code with type ${type} and data ${data} has been scanned!`);
      };

      if (hasPermission === null) {
        return <Text>Requesting for camera permission</Text>;
      }
      if (hasPermission === false) {
        return <Text>No access to camera</Text>;
      }




  return (
    <View>
       <BarCodeScanner
        onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
        style={StyleSheet.absoluteFillObject}
      />
      {scanned && <Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />}
    </View>
  )
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'center',
      },
})

Hi @huntermd111

You will need to rebuild your app after installing expo-barcode-scanner so that it includes the native code from the module.

How do you do that?

How are you running the app now?

Normally you would either run it in Expo Go (which should have the Expo BarCode Scanner native code included) or you would build the app using EAS Build.

If you’re currently running it in Expo Go then there’s a possibility that you have the wrong version of expo-barcode-scanner installer (although I’m not sure how likely it is to get the error you’re seeing if you do that.)

Try running to fix/check the dependency versions:
npx expo install --fix
and:
npx expo-doctor

And also let us know how you’re currently running the app.

I do run expo go and your advice worked I had installed yarn because I hadn’t before and now it works thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.