60

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.)

1
  • 2
    I think it's better to erase the project, re-create it, and copy and paste the code. Commented May 8, 2019 at 1:48

23 Answers 23

40

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Good, but deprecated answer, at the top of docs this note
If you're running on an Expo managed project, the above solution will no longer work in 2021. As documented on docs.expo.dev/guides/testing-with-jest you can now just add yarn add jest-expo --dev
This answer is deprecated! Install it via expo install @react-native-async-storage/async-storage (docs.expo.dev/versions/latest/sdk/async-storage), see the answer from @jonnyg23 below.
16

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.

3 Comments

This is the only valid answer around here!
This works for me too. It's a valid answer.
Thank you - after an entire afternoon of searching - this answer was the solution
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.

Comments

3

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

3 Comments

guy mentioned he is using expo. I think you need to eject first before running react-native run-ios
For non expo users - this is a correct solution to the problem. The better answer explanation would help
"BUNDLER" is the key! You have to rebuild your app UGH
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
    }

2 Comments

rephrase what you are describing, I didn't really understand. in my MainApplication.java looks like this: @Override protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); // Packages that cannot be autolinked yet can be added manually here, for example: // packages.add(new MyReactNativePackage()); return packages; }
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
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.

Comments

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.

Comments

1

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.

Comments

1

for react-native version 0.76.4 the same problem can be seen and the best activity in this case is to downgrade to 0.76.3 so just run npm install [email protected] in terminal

1 Comment

I was using react-native 0.76.4 with "@react-native-async-storage/async-storage": "^1.23.1", but it was not working tried so many solutions but nothing worked, so after downgrading it is working fine
1

I also encountered the same error and was using Expo Go. I decided to make a new build and update our devices that I use for testing:

eas build --platform android --profile development

From what I can check the build version previously installed on my devices that did not have the storage module installed.

This answer will be valid for anyone using EAS

Comments

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

Comments

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.

Comments

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’;

Comments

0

In package.json created link:

    "@react-native-community/async-storage": "https://github.com/react-native-async-storage/async-storage",

Comments

0

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

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

Comments

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

1 Comment

Try to add this line in the file where you add the navigation: import * as React from 'react'; this has worked for me.
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

Comments

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.

2 Comments

npm start --reset-cache npm run android for ios: npm run ios
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.
0
  1. install pods -> pod install
  2. uninstall the app
  3. install the app again.

Comments

0

For folks who are still facing this issue on the latest versions onf Expo (v48 and above)

Rebuild the application and generate the development build. This should resolve the issue for you.

Comments

0
npx expo-doctor

npx expo install --check

just run this two command for dependency conflicts and see magic, it works fine

Comments

0

The @react-native-community/react-native-async-storage is probably being used as a dependency for another library. But it's deprecated.

You should use @react-native-async-storage/async-storage instead.

Comments

0

Encountered an same error in my React Native project. After troubleshooting, I found a solution that resolved the problem. Here’s a step-by-step guide to fix it.

Step-by-Step Fix

  1. Ensure Correct Installation

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

Or, if using Yarn:

**yarn add @react-native-async-storage/async-storage**
  1. Clear Cache and Rebuild Since React Native caches can cause issues, I cleared the cache and restarted my project using the following commands:

    npx react-native start --reset-cache

    npx react-native run-android # For Android

  2. Check for Linking React Native should automatically link the package. However, if I needed to manually link it, I used:

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

  3. Manually Rebuild for Android

For Android, I ensured a clean build by running:

cd android

./gradlew clean

cd ..

npx react-native run-android

After making these changes, my AsyncStorage implementation worked correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.