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

Expo iOS独立构建无法加载yml文件资源问题求助

iOS独立包构建时YML文件读取失败的解决办法

问题说明

开发的Expo跨端应用包含大量YML格式资源文件,在ExpoGo预览、Android构建包中都能正常运行,但用eas build构建iOS独立包时,会触发读取错误:

Error: Calling the 'readAsStringAsync' function has failed
→ Caused by: File '/Users/johnDoe/Library/Developer/CoreSimulator/Devices/B463AF76-44EB-4DEF-9DB8-45BCA9B52BC1/data/Containers/Bundle/Application/BE917BA8-E697-4645-A5BA-4C113E222627/Example.app/assets/assets/index.yml' is not readable

目前字体、图片等资源加载正常,当前metro.config.js配置如下:

const { getDefaultConfig } = require('expo/metro-config')

const config = getDefaultConfig(__dirname)
config.resolver.assetExts.push('yml')

module.exports = config

修复步骤

  • 明确声明YML资源目录
    在app.json或app.config.js的expo节点下添加assets配置,指定YML文件的位置,让Expo打包iOS时正确复制文件并设置权限:

    {
      "expo": {
        "assets": [
          "./assets/**/*.yml"
        ]
      }
    }
    
  • 调整Metro对YML的处理规则(若上述方法无效)
    把YML从静态资源列表移到源码扩展名列表,让Metro将其作为源码打包,避免资源路径权限问题:

    const { getDefaultConfig } = require('expo/metro-config')
    
    const config = getDefaultConfig(__dirname)
    // 移除assetExts中的yml
    config.resolver.assetExts = config.resolver.assetExts.filter(ext => ext !== 'yml')
    // 添加到sourceExts
    config.resolver.sourceExts.push('yml')
    
    module.exports = config
    

    注意:这种方式需要配合babel插件(如babel-plugin-transform-yaml-import)来支持YML的import/require,直接获取文件内容。

  • 修正文件读取路径
    调用readAsStringAsync时,不要硬编码路径,用Expo的Asset API获取正确URI:

    import { Asset } from 'expo-asset';
    import ymlFile from './assets/index.yml';
    
    const asset = Asset.fromModule(ymlFile);
    await asset.downloadAsync();
    const content = await readAsStringAsync(asset.localUri || asset.uri);
    
  • 清理构建缓存
    执行命令清除EAS和Metro的缓存,避免旧配置残留:

    eas build --clear-cache
    npx expo start --clear
    

内容的提问来源于stack exchange,提问作者Mark Rofail

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 13:48:10