You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native导入react-native-fs报错:无法读取null的'RNFSFIleTypeRegular'属性

错误原因及解决方案

错误原因

你遇到的Cannot read property 'RNFSFIleTypeRegular' of null错误,核心原因是你的项目基于Expo Managed Workflow,而react-native-fs是依赖原生代码的第三方库,在Expo的托管模式下无法正常工作:

  • Expo Managed Workflow不允许直接修改iOS/Android原生项目文件,自动链接机制无法在这种环境下生效,导致RNFS模块初始化失败,最终返回null,访问其属性时就会抛出该错误。
  • 另外,react-native-fs@2.20.0与你使用的React Native 0.71.3(Expo 48对应版本)也存在一定的兼容性适配问题,但本质还是Expo环境的限制。

解决方案

方案一:改用Expo官方文件系统API(推荐)

Expo提供了官方的expo-file-system库,完全适配Managed Workflow,无需原生链接,功能覆盖react-native-fs的大部分常用场景:

  1. 安装依赖:
npx expo install expo-file-system
  1. 替换代码中的react-native-fs调用:
    比如你注释的文件保存逻辑,可以修改为:
import * as FileSystem from 'expo-file-system';

// ...

const handleDownload = () => {
  // ... 之前生成file的逻辑不变
  const path = FileSystem.documentDirectory + 'ameya-test.xlsx';
  FileSystem.writeAsStringAsync(path, file, { encoding: FileSystem.EncodingType.Base64 })
    .then(() => {
      Alert.alert('Success', 'Excel file saved!');
    })
    .catch((error) => {
      console.error('Error saving file:', error);
    });
};

方案二:切换到Expo Bare Workflow

如果必须使用react-native-fs,可以将项目转为Bare Workflow,允许使用原生模块:

  1. 生成原生项目文件:
npx expo prebuild
  1. 重新安装依赖并运行:
npm install
# 运行Android
npm run android
# 运行iOS
npm run ios

注意:切换后会失去Expo Managed的部分便利性,需要自行维护原生项目配置。

内容的提问来源于stack exchange,提问作者Paras Bansal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:57:49