React Native链接第三方库失败,报错Cannot read property '_text' of undefined
Hey there, let's work through this linking error you're hitting with react-native-fetch-blob (and all other third-party libraries, it seems). The error message you're seeing—ERRPACKAGEJSON No package found. Are you sure this is a React Native project? Cannot read property '_text' of undefined—usually points to issues with your project structure, package.json integrity, or tooling state. Here are step-by-step fixes to try:
1. Double-check you're in the right directory
First off, make sure you're running the react-native link command from the root directory of your React Native project. This is the folder that contains your package.json file. To confirm:
- On Mac/Linux: Run
pwdand verify the path matches your project's root. - On Windows: Run
cdto see your current directory.
If you're in a subfolder (like android/ or ios/), switch back to the root with cd .. as needed, then re-run the link command.
2. Verify your package.json exists and is valid
- Check if
package.jsonis present in your project root. If it's missing, you might not be in a React Native project, or the file was accidentally deleted. - If the file exists, check for syntax errors (like missing commas, mismatched brackets). You can validate it quickly with:
Fix any errors it flags, then try linking again.npm validate
3. Clean dependencies and npm cache
Corrupted node_modules or cached data often cause these kinds of issues. Let's reset your dependency state:
- Delete the
node_modulesfolder:# Mac/Linux rm -rf node_modules # Windows (Command Prompt) rd /s /q node_modules - Clear the npm cache:
npm cache clean --force - Reinstall all dependencies:
npm install - Now try linking again:
react-native link react-native-fetch-blob
4. Update your React Native CLI (or use npx)
Older versions of the React Native CLI might have compatibility bugs. Try either:
- Update the global CLI:
npm install -g react-native-cli - Or use
npxto run the latest CLI version without global installation:npx react-native link react-native-fetch-blob
5. Fall back to manual linking
If automatic linking still fails, you can link react-native-fetch-blob manually for each platform:
For iOS
- Open your Xcode project (
.xcworkspacefile) in theios/folder. - Drag
node_modules/react-native-fetch-blob/ios/RNFetchBlob.xcodeprojinto the Libraries group of your project. - Go to your target's Build Phases > Link Binary With Libraries, then click the
+button and addlibRNFetchBlob.a.
For Android
- Open
android/settings.gradleand add these lines:include ':react-native-fetch-blob' project(':react-native-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fetch-blob/android') - Open
android/app/build.gradleand add this line to thedependenciesblock:implementation project(':react-native-fetch-blob') - Open
android/app/src/main/java/[your-package-name]/MainApplication.java:- Add the import:
import com.RNFetchBlob.RNFetchBlobPackage; - In the
getPackages()method, addnew RNFetchBlobPackage()to the returned list.
- Add the import:
6. Confirm your project is a valid React Native project
Last check: Make sure your project has the standard React Native structure—you should see android/, ios/, index.js (or App.js), and of course package.json in the root. If any of these are missing, you might be working in a non-RN project directory.
内容的提问来源于stack exchange,提问作者Ravi

