React Native File System (react-native-fs)
react-native-fs provides comprehensive native filesystem access for React Native applications across iOS, Android, and Windows platforms. It offers a robust API for common file operations such as reading, writing, and deleting files, managing directories, and facilitating advanced use cases like background file downloads and uploads with progress tracking. The library is actively maintained, with the current stable version being 2.20.0, and receives regular updates to address compatibility with new React Native versions and underlying OS changes, exemplified by recent fixes for iOS crashes and Android type corrections. A key differentiator is its direct native integration, enabling features like handling content URIs on Android and asset video URIs on iOS, which are crucial for complex media and document management within mobile applications. It carefully manages compatibility across various React Native versions, requiring specific `react-native-fs` versions depending on the host `react-native` and Gradle versions, highlighting its sensitivity to the broader ecosystem.
Common errors
-
Invariant Violation: `new NativeEventEmitter()` was called with a non-null argument without the native module implementing `addListener` and `removeListeners`.
cause Using `react-native-fs` with a React Native version that expects `NativeEventEmitter` compliance, but the installed `react-native-fs` version is too old.fixUpgrade `react-native-fs` to version `2.19.0` or newer: `npm install react-native-fs@latest` or `yarn add react-native-fs@latest`. -
Undefined symbol: _OBJC_CLASS_$_RCTImageLoader
cause iOS build failure due to changes in React Native's internal `RCTImageLoader` import paths (specifically for RN > 0.60).fixUpgrade `react-native-fs` to version `2.15.1` or higher, which includes a fix for this issue by importing `RCTImageLoaderProtocol`. -
TypeError: Cannot read property 'DocumentDirectoryPath' of undefined
cause The `RNFS` native module was not correctly linked or initialized, or the JavaScript import is incorrect.fixEnsure the library is properly linked (using `react-native link react-native-fs` or manual linking as per documentation). Verify the import statement is `import RNFS from 'react-native-fs';`. -
Task 'assembleRelease' failed. Android resource linking failed.
cause Common Android build issue, often related to AndroidX migration status or incorrect Gradle configuration.fixVerify that your `react-native-fs` version supports your AndroidX migration status (v2.14.0+ for AndroidX). Check your `android/app/build.gradle` and `android/settings.gradle` for correct `react-native-fs` package inclusion, especially if using manual linking.
Warnings
- breaking Breaking changes in compatibility with React Native versions require specific `react-native-fs` versions. Failure to install the correct version will lead to build errors or runtime crashes.
- breaking `react-native-fs` versions prior to `2.14.0` do not support AndroidX. Projects migrating to AndroidX must upgrade.
- breaking The `size` property returned by file `stat` operations changed from `Int` to `Double` for increased precision, particularly affecting larger files.
- gotcha On Android, the `DocumentDirectory` is strictly mapped to the application's private `files` directory. This differs from iOS, where it might have broader permissions or map to different system directories. Access to external storage requires additional permissions and may not be directly supported through this path.
- gotcha iOS background downloads require specific setup and careful handling of sessions. Improper configuration can lead to downloads failing or not resuming correctly.
- gotcha Older versions of `react-native-fs` (< 2.19.0) did not properly implement `addListener` and `removeListeners` methods, leading to `NativeEventEmitter` warnings in newer React Native environments.
Install
-
npm install react-native-fs -
yarn add react-native-fs -
pnpm add react-native-fs
Imports
- RNFS
import { RNFS } from 'react-native-fs';import RNFS from 'react-native-fs';
- DocumentDirectoryPath
import { DocumentDirectoryPath } from 'react-native-fs';import RNFS from 'react-native-fs'; const path = RNFS.DocumentDirectoryPath;
- RNFS (CommonJS)
const RNFS = require('react-native-fs');
Quickstart
import RNFS from 'react-native-fs';
const fileName = 'test.txt';
const content = 'Hello from react-native-fs!';
const filePath = `${RNFS.TemporaryDirectoryPath}/${fileName}`;
async function runFsExample() {
try {
// 1. Write a file
await RNFS.writeFile(filePath, content, 'utf8');
console.log(`File written to: ${filePath}`);
// 2. Read the file
const readContent = await RNFS.readFile(filePath, 'utf8');
console.log(`File content: ${readContent}`);
if (readContent !== content) {
throw new Error('Content mismatch!');
}
// 3. Check if file exists
const exists = await RNFS.exists(filePath);
console.log(`File exists: ${exists}`);
// 4. Get file stats
const statResult = await RNFS.stat(filePath);
console.log(`File size: ${statResult.size} bytes`);
// 5. Delete the file
await RNFS.unlink(filePath);
console.log(`File deleted: ${filePath}`);
const deletedExists = await RNFS.exists(filePath);
console.log(`File exists after deletion: ${deletedExists}`);
} catch (error) {
console.error('An error occurred during FS operations:', error);
}
}
runFsExample();