使用Expo CLI创建启动页时遭遇开发服务器500错误求助
Expo开发服务器返回500错误(启动页配置问题)
针对你遇到的问题,尝试以下解决方案:
确保依赖版本匹配
由于你使用的是Expo SDK 46,直接运行以下命令重新安装对应版本的依赖,避免版本不兼容:expo install expo-splash-screen expo-font @expo/vector-icons修正启动页阻止隐藏的代码逻辑
原代码中SplashScreen.preventAutoHideAsync()没有错误捕获,且放在组件顶层可能导致异步操作失败引发服务器错误。将其移至初始化函数的try块内,并添加完整错误处理:import React, { useCallback, useEffect, useState } from 'react'; import { Text, View } from 'react-native'; import Entypo from '@expo/vector-icons/Entypo'; import * as SplashScreen from 'expo-splash-screen'; import * as Font from 'expo-font'; export default function App() { const [appIsReady, setAppIsReady] = useState(false); useEffect(() => { async function prepare() { try { // 将阻止自动隐藏操作移入try块,捕获异常 await SplashScreen.preventAutoHideAsync(); await Font.loadAsync(Entypo.font); // 移除不必要的模拟延迟(若不需要测试加载状态) // await new Promise(resolve => setTimeout(resolve, 2000)); } catch (e) { console.error('资源初始化失败:', e); } finally { setAppIsReady(true); } } prepare(); }, []); const onLayoutRootView = useCallback(async () => { if (appIsReady) { try { await SplashScreen.hideAsync(); } catch (e) { console.error('隐藏启动页失败:', e); } } }, [appIsReady]); if (!appIsReady) { return null; } return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }} onLayout={onLayoutRootView}> <Text>SplashScreen Demo! 👋</Text> <Entypo name="rocket" size={30} /> </View> ); }清除缓存重启开发服务器
运行以下命令清除Expo缓存并重启服务,解决缓存导致的异常:expo start -c检查启动页配置文件
确认app.json或app.config.js中的splash配置正确,图片路径存在且格式符合要求:{ "expo": { "splash": { "image": "./assets/splash.png", "resizeMode": "contain", "backgroundColor": "#ffffff" } } }验证Node.js版本
Expo SDK 46推荐使用Node.js 14.x或16.x版本,版本过高/过低可能引发兼容性问题,建议切换至对应版本后重试。
内容的提问来源于stack exchange,提问作者Samin Jilani
相关产品推荐
相关产品推荐

