React Native+Expo浏览器模式运行报错:模块找不到问题求助
问题
作为React Native新手,使用Expo CLI浏览器模式运行项目时遇到两个模块找不到的错误:
Module not found: Can't resolve '../Utilities/Platform'Module not found: Can't resolve './PlatformColorValueTypes'
同时本地页面http://localhost:19006/无任何内容。
App.js代码如下:
import { StatusBar } from "expo-status-bar"; import React from "react"; import { StyleSheet, Text, View } from "react-native"; import { borderColor } from "react-native/Libraries/Components/View/ReactNativeStyleAttributes"; export default function App() { return ( <View style={styles.container}> <View style={styles.box}> <View style={styles.moziBox}></View> <View style={styles.gazoBox}></View> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "#eee", alignItems: "center", justifyContent: "center", }, box: { height: 100, width: "100%", borderColor: "lightblue", borderWidth: 1, }, moziBox: { flex: 1, backgroundColor: "blue", padding: 16, }, gazoBox: { width: 100, backgroundColor: "red", }, });
已确认依赖版本:
- react@18.2.0
- react-native@0.70.4
解决方案
1. 删除内部私有模块的导入
你的代码中直接引用了React Native的内部私有模块react-native/Libraries/Components/View/ReactNativeStyleAttributes,而Expo的Web适配层不支持这类内部文件的直接导入,这是引发错误的核心原因。
直接删除这行无用的导入代码:
import { borderColor } from "react-native/Libraries/Components/View/ReactNativeStyleAttributes";
2. 清除缓存并重启项目
模块查找错误通常和缓存冲突有关,按以下步骤操作:
- 关闭当前运行的Expo开发服务
- 执行命令清除缓存并重启:
npx expo start --clear - 在启动菜单中选择浏览器模式(按
w键)
3. 修复版本兼容性问题
React Native 0.70.4对应的Expo SDK版本应为47.x,版本不匹配会导致Web端适配异常:
- 运行
npx expo install --fix自动修复依赖版本不兼容问题 - 若自动修复无效,可手动在
package.json中指定expo版本为^47.0.0,然后执行npm install重新安装依赖
4. 确认Web平台配置
确保项目已开启Web平台支持:
- 检查
app.json(或app.config.js)中expo.platforms包含web:
{ "expo": { "platforms": ["ios", "android", "web"] } }
内容的提问来源于stack exchange,提问作者dixhom
相关产品推荐
相关产品推荐

