react-native-async-storage / async-storage

An asynchronous, persistent, key-value storage system for React Native.
https://react-native-async-storage.github.io/async-storage/
MIT License
4.61k stars 457 forks source link

Error: [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null #1105

Closed vkarkhanis closed 1 week ago

vkarkhanis commented 1 week ago

What happened?

I am trying to use Firebase with React Native and using expo. If I use following code I get a warning asking me to use ReactNativeAsyncStorage:

export const FIREBASE_APP = initializeApp(FirebaseConfig);
export const FIREBASE_AUTH = getAuth(FIREBASE_APP);

Howeve if I install @react-native-async-storage/async-storage, and use it as below:

import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage';
import { initializeApp } from 'firebase/app';
import { getReactNativePersistence, initializeAuth } from 'firebase/auth';
import FirebaseConfig from './firebase.config';

export const FIREBASE_APP = initializeApp(FirebaseConfig);
export const FIREBASE_AUTH = initializeAuth(FIREBASE_APP, {
  persistence: getReactNativePersistence(ReactNativeAsyncStorage),
});

my app does not start and instead throws an error:


 ERROR  Error: [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

To fix this issue try these steps:

  • Uninstall, rebuild and restart the app.

  • Run the packager with `--reset-cache` flag.

  • If you are using CocoaPods on iOS, run `pod install` in the `ios` directory, then rebuild and re-run the app.

  • Make sure your project's `package.json` depends on `@react-native-async-storage/async-storage`, even if you only depend on it indirectly through other dependencies. CLI only autolinks native modules found in your `package.json`.

  • If this happens while testing with Jest, check out how to integrate AsyncStorage here: https://react-native-async-storage.github.io/async-storage/docs/advanced/jest

If none of these fix the issue, please open an issue on the GitHub repository: https://github.com/react-native-async-storage/async-storage/issues
, 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

My dependencies in package.json are as below: "dependencies": { "@react-native-async-storage/async-storage": "1.23.1", "@react-navigation/bottom-tabs": "^6.5.20", "@react-navigation/core": "^6.4.16", "@react-navigation/native": "^6.1.17", "@react-navigation/stack": "^6.3.29", "axios": "^1.7.2", "expo": "~51.0.8", "expo-dev-client": "~4.0.14", "expo-status-bar": "~1.12.1", "firebase": "^10.12.2", "lodash": "^4.17.21", "react": "18.2.0", "react-native": "0.74.1", "react-native-gesture-handler": "~2.16.1", "react-native-get-random-values": "^1.11.0", "react-native-safe-area-context": "4.10.1", "react-native-screens": "3.31.1", "react-native-toast-message": "^2.2.0", "uuid": "^9.0.1" }, "devDependencies": { "@babel/core": "^7.20.0", "@types/lodash": "^4.17.5", "@types/react": "~18.2.45", "typescript": "~5.3.3", "@types/axios": "^0.14.0", "@types/uuid": "^9.0.8", }, "private": true

This is happening on Android device. Based on lot of threads related to this, I have tried cleaning node_modules and yarn.lock multiple times but to no avail. I have also made sure I installed @react-native-async-storage/async-storage using: npx expo install @react-native-async-storage/async-storage

I have tried both yarn and npm Nothing seems to be working

Version

1.23.1

What platforms are you seeing this issue on?

System Information

info Fetching system and libraries information...
System:
  OS: Windows 11 10.0.22621
  CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Memory: 4.78 GB / 15.86 GB
Binaries:
  Node:
    version: 20.11.0
    path: ~\software\technical\node\install\node.EXE
  Yarn:
    version: 1.22.17
    path: ~\AppData\Roaming\npm\yarn.CMD
  npm:
    version: 10.2.4
    path: ~\software\technical\node\install\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
  Windows SDK: Not Found
IDEs:
  Android Studio: Not Found
  Visual Studio: Not Found
Languages:
  Java: 17.0.1
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.1
    wanted: 0.74.1
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

info React Native v0.74.2 is now available (your project is running on v0.74.1).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.74.2
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.74.1
info For more info, check out "https://reactnative.dev/docs/upgrading?os=windows".

Steps to Reproduce

  1. Create a React Native project using Expo
  2. Add FIrebase Depenency and add the Firebase code mentioned in the problem statement
  3. Start Metro using npm start -- --reset-cache
  4. Scan QR code using Expo Go and wait for app bundling on Android device
  5. The issue occurs as soon as BUndling completes
vkarkhanis commented 1 week ago

Alright, so here is what was needed. In case of Expo Go too, you will need to rebuild because the native code needs to be bundled

So in case of Android, I executed: expo run:android

It took a long time to build first time but subsequent builds were normal. Preferably connect an actual device on USB. Once built, everything worked like a charm. Make sure, earlier version of App is uninstalled, else you may face version incompatibility. Once re-built, subsequently you can work in development mode by executing expo start