React Native File System (react-native-fs)

2.20.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates basic file operations: writing a string to a temporary file, reading its content, checking its existence, retrieving its statistics, and finally deleting it. It uses `RNFS.TemporaryDirectoryPath` for cross-platform compatibility.

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();

view raw JSON →