48

I'm working on a React Native project created with Expo. I've been using regular old AsyncStorage, importing from react-native, and all has been well.

In looking up how to mock AsyncStorage for testing, I saw that react-native-community/react-native-async-storage has its own mock built in.

So I installed the community plugin with yarn add and switched out all my import statements.

When I run my app, I'm getting an error (which I'm retyping myself, excuse some ellipses):

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

To fix this issue try these steps:

-Run `react-native link @react-native-community/async-storage` in the project root.
-Rebuild and restart the app
-Run the packager with `--clearCache` flag.
-If you are using CocoaPods on iOS...
-If this happens while testing with Jest...

So I tried running react-native link @react-native-community/async-storage but I keep getting this error:

Something went wrong while linking. Error: Cannot read property 'pbxprojPath' of null

Some research showed me that Expo apps can't (and don't need to) link.

I tried npm start --clearCache to no avail.

Also, I don't have an ios (or android) folder in my project. This has always been kind of confusing for me because I see it referenced all over the place. I run my app in the Simulator/Emulator (and device) through the Expo app. Once I tried ejecting and there were problems. So, I don't have an ios folder.

(I'll go back to using the old native AsyncStorage from react-native and creating a mock myself, but I'd like to know how to solve this, and similar problems that may arise in the future.)

Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129

17 Answers17

38

This error can be rectified by linking the module like mentioned above but linking does not work if your app is Expo Managed (created via expo-cli).

Assuming your app is Expo managed, instead of using the AsyncStorage from Facebook RN Documentation:

import AsyncStorage from '@react-native-community/async-storage';

Do:

import { AsyncStorage } from 'react-native';

Check documentation here: AsyncStorage Expo

Tadiwanashe
  • 1,246
  • 14
  • 15
  • this solution also doesn't work for me, it's not updating data in first time, please help I am working in a project whose deadline id 5 march means tomorrow. – Anuj Sharma Mar 04 '20 at 10:02
  • 4
    Good, but deprecated answer, at the top of docs this note – Oleg Reym Jan 07 '21 at 07:37
  • If you're running on an Expo managed project, the above solution will no longer work in 2021. As documented on https://docs.expo.dev/guides/testing-with-jest/ you can now just add `yarn add jest-expo --dev` – Niko Dunk Sep 17 '21 at 20:27
  • 1
    This answer is deprecated! Install it via `expo install @react-native-async-storage/async-storage` (https://docs.expo.dev/versions/latest/sdk/async-storage/), see the answer from @jonnyg23 below. – Tobias Kaufmann Sep 21 '21 at 18:48
15

What resolved this for me was to first install the following library:

expo install @react-native-async-storage/async-storage

Then, update your import as such:

import AsyncStorage from '@react-native-async-storage/async-storage'

Note: You may have to restart Expo after this fix.

jonnyg23
  • 229
  • 2
  • 11
5

I just had a similar issue and found the answer provided by Krizzu on github very helpful. Here is what he says: 'If you're using expo, you cannot add this package. Use whatever expo gives you (and ignore the warning about deprecation).' and 'You cannot add additional native modules to expo, unless you eject your project.'

You can read more about this issue following the threads on: https://github.com/react-native-community/react-native-async-storage/issues/89 and https://github.com/react-native-community/react-native-async-storage/issues/72. I hope this helps.

Munishkin
  • 66
  • 5
2

I had the same issue, the reason was cuz I was using react-native-navigation and it messed up the android section in MainApplication.java, so I had to change the file manually, I had already run this command:

react-native link @react-native-community/async-storage

now I have this:

   private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(new MainReactPackage(),
            new AsyncStoragePackage()); //<----- here
        }

        @Override
        protected String getJSMainModuleName() {
            return "index";
        }
    };

and this :

    protected List<ReactPackage> getPackages() {
        // Add additional packages you require here
        // No need to add RnnPackage and MainReactPackage
        return Arrays.<ReactPackage>asList(
                // eg. new VectorIconsPackage()                    
                new AsyncStoragePackage()); //<----- here
    }
Ehsan Zargar Ershadi
  • 24,115
  • 17
  • 65
  • 95
  • 1
    rephrase what you are describing, I didn't really understand. in my ```MainApplication.java``` looks like this: ```@Override protected List getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List packages = new PackageList(this).getPackages(); // Packages that cannot be autolinked yet can be added manually here, for example: // packages.add(new MyReactNativePackage()); return packages; }``` – hanan Jul 28 '20 at 13:23
  • are you suggesting to remove this piece of code and replace with yours? or append them? both methods didn't work for me! note I am also using ```react-navigation``` and facing same issue here – hanan Jul 28 '20 at 13:24
2

Just close the Metro bundler and run react-native run-android or run-ios and it will be fixed.

vanskins
  • 75
  • 6
2

Let's try with @react-native-async-storage/async-storage

And if still error I think you will follow the step in this link

  • remove the app in the Ios simulator (this is the main step for fix the error)
  • then cd ios/ && pod install
  • if React Native <= 0.59 run react-native link @react-native-async-storage/async-storage.
  • run npm start --reset-cache
  • reinstall app with react-native run-ios

Hope that can help you.

tuanngocptn
  • 1,241
  • 13
  • 21
1

I faced the same problem while using react native AsyncStorage. Try:

cd ios && pod install || npx pod-install

Note: For react native version higher than 0.60, linking is automatic.

Then just restart the app.

npm start --reset-cache

App should start up and running again.

Piyush Chandra
  • 159
  • 1
  • 4
0

running

react-native link @react-native-async-storage/async-storage

after this running Metro with "--reset-cache" verbose and running "react-native run-android" in a fresh cmd resolved my issue

irzum shahid
  • 181
  • 2
  • 12
0

Have a try by running the project again by

  • npx react-native run-android command.

If you are using the iOS Device then:

  • cd ios && pod install

  • npx react-native run-ios

if still not works try installing the npm package again and follow the official documentation.

Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
0

In node_modules/@react-native-async-storage/async-storage”/async-storage/src/AsyncStorage.native.js, replace import RCTAsyncStorage from ‘./RCTAsyncStorage’ by import RCTAsyncStorage from ‘./RCTAsyncStorage.expo’;

houcin olmostaf
  • 191
  • 1
  • 12
0

In package.json created link:

    "@react-native-community/async-storage": "https://github.com/react-native-async-storage/async-storage",
Yanov
  • 655
  • 7
  • 13
0

Autolink npx pod-install didn't work for me.

I had to follow the steps for manual linking to eliminate the error.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
0

This issue is mostly happen when node_modules folder has been changed by installing new library or some sort of action. Please try removing cache and do yarn install or npm install again

$ npx react-native-clean-project
$ yarn install
0

try cleaning cache and and restart the server, or you can rebuild the app

npm start --reset-cache
npm run android 
for ios: npm run ios
0

Have a try by running the project again by

npx react-native run-android command. If you are using the iOS Device then:

cd ios && pod install

npx react-native run-ios

if still not works try installing the npm package again and follow the official documentation.

  • npm start --reset-cache npm run android for ios: npm run ios – Muhammad Arslan ali Aug 26 '22 at 15:07
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 14:48
0
  1. install pods -> pod install
  2. uninstall the app
  3. install the app again.
jose920405
  • 7,982
  • 6
  • 45
  • 71
0

I tried the solutions above. My project is expo based react native project.

The only thing that worked was to remove the ios folder. And rebuild everything using npx expo run:ios.

If this doesnt work you could try again with expo install @react-native-async-storage/async-storage and then remove ios folder and rebuild.

jsbisht
  • 9,079
  • 7
  • 50
  • 55