在EAS服务器构建APK时react-native-maps报错:导入Web不支持的原生模块
解决EAS构建APK时react-native-maps的web模块导入报错问题
报错核心是expo-router在打包server manifest时误触发了web环境的模块检测,即便你目标是Android平台。以下是几个可行的解决方法:
明确指定构建平台
执行EAS构建时加上--platform android参数,确保构建流程完全针对原生平台,避免触发web打包逻辑:eas build --platform android条件导入react-native-maps组件
在代码里通过Platform判断环境,只在原生端导入相关组件,避免web端打包时加载原生模块:import { Platform } from 'react-native'; let MapView, Marker; if (Platform.OS !== 'web') { ({ MapView, Marker } = require('react-native-maps')); } // 使用时先判断组件是否存在 export default function MapScreen() { if (!MapView) return null; return <MapView><Marker /></MapView>; }调整Babel配置忽略web端的react-native-maps
在babel.config.js中添加环境判断,仅在web生产环境将react-native-maps映射为空模块:module.exports = function(api) { api.cache(true); return { presets: ['babel-preset-expo'], env: { production: { plugins: [ process.env.EXPO_PLATFORM === 'web' && [ 'module-resolver', { alias: { 'react-native-maps': './empty-module.js', }, }, ], ].filter(Boolean), }, }, }; };然后在项目根目录创建
empty-module.js:export default {};更新依赖版本并清理缓存
确保expo和react-native-maps版本兼容,执行更新命令:npm install expo@latest react-native-maps@latest之后清理EAS构建缓存再重新构建:
eas build --clear-cache --platform android
内容的提问来源于stack exchange,提问作者Eric
相关产品推荐
相关产品推荐

