已安装@react-native-async-storage仍报AsyncStorage错误,如何定位依赖来源?
问题描述
错误信息:
ERROR Invariant Violation: AsyncStorage has been removed from react-native core. - js engine: hermes

我已经安装了@react-native-async-storage/async-storage,但仍持续出现上述AsyncStorage相关错误,请问有什么方法可以定位触发该错误的依赖包?
我的package.json依赖如下:
{ "dependencies": { "@react-native-async-storage/async-storage": "^1.17.11", "@react-native-community/blur": "^3.4.1", "@react-native-community/netinfo": "^9.0.0", "@react-native-masked-view/masked-view": "^0.2.7", "@sentry/react-native": "1.4.1", "@tekion/tekion-base": "5.13.2", "axios": "0.18.0", "babel-plugin-transform-inline-environment-variables": "^0.4.3", "currencyformatter.js": "^2.2.0", "dinero.js": "^1.8.1", "fast-memoize": "^2.5.1", "immer": "^2.0.0", "lodash": "^4.17.11", "lottie-ios": "3.1.8", "lottie-react-native": "3.5.0", "moment": "^2.24.0", "moment-timezone": "^0.5.25", "prop-types": "15.6.2", "pusher-js": "^5.1.1", "qs": "^6.7.0", "react": "18.2.0", "react-native": "^0.71.0", "react-native-blob-util": "0.17.3", "react-native-code-push": "8.0.1", "react-native-device-info": "10.6.0", "react-native-exception-handler": "^2.10.7", "react-native-fs": "^2.18.0", "react-native-gesture-handler": "^2.9.0", "react-native-idle-timer": "^2.1.6", "react-native-keyboard-aware-scroll-view": "^0.9.1", "react-native-modal": "^13.0.0", "react-native-orientation-locker": "^1.4.0", "react-native-pdf": "6.4.0", "react-native-reanimated": "^3.1.0", "react-native-safe-area-context": "^4.4.1", "react-native-screens": "^3.20.0", "react-native-splash-screen": "^3.2.0", "react-native-version-check": "^3.4.6", "react-native-webview": "^11.23.0", "react-navigation": "^4.4.3", "react-navigation-drawer": "^1.4.0", "react-navigation-redux-helpers": "2.0.9", "react-navigation-stack": "^1.7.3", "react-redux": "6.0.0", "redux": "4.0.1", "redux-actions": "^2.6.5", "redux-persist": "^5.10.0", "redux-thunk": "2.3.0", "reselect": "^4.0.0", "tmobcomponents": "git+https://gitlab.com/myRepo/tmobcomponents#rn-sixtyone-upg-1" }, "devDependencies": { "@babel/core": "^7.12.9", "@babel/preset-env": "^7.14.0", "@babel/runtime": "^7.12.5", "@react-native-community/eslint-config": "^3.0.0", "@tsconfig/react-native": "^2.0.2", "@types/jest": "^29.2.1", "@types/react": "^18.0.24", "@types/react-test-renderer": "^18.0.0", "babel-eslint": "^10.0.1", "babel-jest": "^29.2.1", "babel-plugin-module-resolver": "^3.2.0", "eslint": "^8.19.0", "eslint-config-airbnb": "^17.1.0", "eslint-import-resolver-babel-module": "^5.0.1", "eslint-plugin-import": "^2.16.0", "eslint-plugin-jsx-a11y": "^6.2.1", "eslint-plugin-react": "^7.12.4", "husky": "^1.3.1", "jest": "^29.2.1", "lint-staged": "^8.1.5", "metro-react-native-babel-preset": "^0.73.7", "prettier": "^2.4.1", "react-test-renderer": "18.2.0", "typescript": "4.8.4" }, "jest": { "preset": "react-native" } }
解决方法
以下是几种定位触发错误依赖包的实用方法:
1. 查看错误完整堆栈
错误弹出时,拉取完整的堆栈追踪信息,里面会显示调用旧版AsyncStorage的具体文件路径。如果是第三方依赖,路径会包含node_modules下的对应包名,直接锁定目标依赖。
2. 全局搜索旧版AsyncStorage导入
在项目根目录执行命令,遍历node_modules查找所有从react-native导入AsyncStorage的代码:
grep -r "import.*AsyncStorage.*from.*react-native" ./node_modules
执行后会直接列出仍在使用旧版API的依赖包。
3. 重点排查高风险依赖
从你的依赖列表来看,以下几个包大概率是问题源头:
redux-persist@5.10.0:旧版本默认依赖react-native内置的AsyncStorage,需手动配置替换为新包。react-navigation系列包(如react-navigation@4.4.3、react-navigation-stack@1.7.3):旧版导航库可能内置了旧的存储逻辑。@tekion/tekion-base@5.13.2和tmobcomponents:内部封装的工具可能未适配新的AsyncStorage,需检查源码或更新版本。
4. 临时替换并添加调用日志
在项目入口文件(如index.js)中添加以下代码,替换全局旧版AsyncStorage并记录调用来源:
import { AsyncStorage as RNAsyncStorage } from 'react-native'; import AsyncStorage from '@react-native-async-storage/async-storage'; // 替换旧API并打印调用栈 Object.defineProperty(RNAsyncStorage, 'getItem', { value: (...args) => { console.error('旧版AsyncStorage调用来源:', new Error().stack); return AsyncStorage.getItem(...args); }, });
运行项目后,控制台会输出所有调用旧API的堆栈,精准定位依赖。
5. 逐步移除依赖排查
如果以上方法没找到,可尝试逐步移除可疑依赖,每次移除后重新运行项目,观察错误是否消失,以此确定问题依赖。
内容的提问来源于stack exchange,提问作者Lokesh
相关产品推荐
相关产品推荐

