将React Native项目迁移至Web时遇模块找不到错误求助
解决React Native迁移Web时react-native-maps的codegenNativeCommands模块找不到错误
这个错误是因为codegenNativeCommands是React Native原生端的代码生成工具模块,Web环境中不存在,需要给它配置别名指向你自己的占位文件,具体步骤如下:
创建占位文件
在项目src/mocks目录下新建codegenNativeCommands.js,内容可以是最基础的空实现:// src/mocks/codegenNativeCommands.js export default function codegenNativeCommands() { return {}; }如果后续发现有更多依赖需求,再补充对应的导出内容即可。
更新config-overrides.js的别名配置
在你的config-overrides.js中,把缺失的模块路径映射到刚才创建的占位文件:const path = require('path'); module.exports = function override(config, env) { // 保留你原有的别名配置,新增下面这行 config.resolve.alias = { ...config.resolve.alias, 'react-native': 'react-native-web', 'react-native/Libraries/Utilities/codegenNativeCommands': path.resolve(__dirname, 'src/mocks/codegenNativeCommands.js'), // 你的其他占位组件别名,比如react-native-maps的映射 'react-native-maps': path.resolve(__dirname, 'src/mocks/MapView.js') }; return config; };额外检查
如果运行后还有其他react-native-maps相关的模块缺失,按照同样的逻辑,为对应的原生模块创建占位文件并配置别名即可。如果占位方案不够稳定,也可以考虑替换为Web端兼容的地图组件(比如直接使用Leaflet、Google Maps JS API等)来彻底替代react-native-maps。
内容的提问来源于stack exchange,提问作者Kurt J
相关产品推荐
相关产品推荐

