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

React Native Expo应用Web打包报错:无法解析Platform模块求助

解决Expo Web版本无法解析Platform模块的问题

以下是针对你遇到的无法从react-native/Libraries/Components/TextInput/TextInputState.js中解析模块../../Utilities/Platform错误的具体解决步骤:

方案1:更新依赖并清除缓存

  • 先升级Expo及Web相关依赖到最新稳定版:
    npm install expo@latest expo-web@latest react-native-web@latest
    
  • 清除Expo缓存后重启服务:
    npx expo start --clear
    

方案2:配置Metro模块解析别名

在项目根目录创建/修改metro.config.js,手动指定Platform模块的Web兼容路径:

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

const config = getDefaultConfig(__dirname);

config.resolver.alias = {
  ...config.resolver.alias,
  'react-native/Libraries/Utilities/Platform': 'react-native-web/dist/exports/Platform',
};

module.exports = config;

修改完成后执行npx expo start --clear重启服务。

方案3:正确配置postinstall脚本修复导入路径

之前的空模块方法有误,可通过脚本自动修改依赖文件的导入路径:

  1. 在项目根目录创建scripts/fix-textinputstate.js,写入以下内容:
    const fs = require('fs');
    const path = require('path');
    
    const targetFile = path.join(__dirname, '../node_modules/react-native/Libraries/Components/TextInput/TextInputState.js');
    let fileContent = fs.readFileSync(targetFile, 'utf8');
    
    // 替换原导入路径为Web兼容的Platform模块路径
    fileContent = fileContent.replace('../../Utilities/Platform', 'react-native-web/dist/exports/Platform');
    fs.writeFileSync(targetFile, fileContent, 'utf8');
    
  2. 在package.json的scripts字段中添加postinstall钩子:
    "scripts": {
      // 保留原有脚本,新增以下内容
      "postinstall": "node scripts/fix-textinputstate.js"
    }
    
  3. 执行npm install触发脚本,之后重启Expo服务。

方案4:降级react-native-web版本

若以上方案无效,可能是版本兼容问题,尝试降级到稳定版本:

npm install react-native-web@0.19.9

执行完成后清除缓存并重启服务。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 13:09:53