Expo Jest报错:无法找到模块'expo/build/winter'的解决方案咨询
解决 "Cannot find module 'expo/build/winter'" 错误
问题根源
你使用的jest-expo ^50.0.1是对应Expo SDK 50的版本,但项目依赖的是expo ^49.0.16,版本不匹配导致高版本jest-expo尝试引用Expo 49不存在的expo/build/winter模块,从而触发报错。
解决方案
1. 降级jest-expo到匹配Expo 49的版本
修改package.json中devDependencies里的jest-expo版本为~49.0.0(与Expo 49 SDK严格对应):
"devDependencies": { // ...其他依赖项 "jest": "^29.7.0", "jest-expo": "~49.0.0", "react-test-renderer": "^18.2.0" }
2. 调整Jest配置,移除无效路径匹配
在package.json的jest配置中,删除transformIgnorePatterns里的expo/build/winter(Expo 49不存在该路径):
"jest": { "preset": "jest-expo", "transformIgnorePatterns": [ "node_modules/(?!(jest-)?react-native|@react-native(-community)?|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)" ], // ...其他配置项 }
3. 彻底重装依赖并清理缓存
执行以下命令完成依赖重置:
# 删除node_modules和锁文件 rm -rf node_modules package-lock.json yarn.lock # 重新安装依赖 npm install # 清理Jest缓存 jest --clearCache
4. 确认测试依赖的位置
jest、jest-expo、react-test-renderer放在devDependencies是正确的——这些工具仅用于开发阶段的测试,无需打包到生产环境,当前配置无问题。
验证
重新运行测试命令确认问题解决:
npm run test
内容的提问来源于stack exchange,提问作者Alice W
相关产品推荐
相关产品推荐

