You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native项目报错找不到@env模块 env已放根目录如何解决

解决方案

  • 首先修正Babel配置错误,你当前配置存在两个问题:一是配置字段写为单数plugin,正确应为复数plugins;二是babel-preset-expo属于预设配置,应该放在presets字段下,不能放在plugins中。修正后的完整配置如下:
module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['module:react-native-dotenv'],
  }
}
  • 配置修改后必须清理Babel缓存重启开发服务,否则配置不会生效:
    • Expo项目执行命令:npx expo start --clear
    • 原生React Native项目执行命令:npx react-native start --reset-cache
  • 确认已安装react-native-dotenv开发依赖,未安装的话执行对应命令:
    • npm环境:npm install react-native-dotenv --save-dev
    • yarn环境:yarn add react-native-dotenv --dev
  • 如果以上操作完成后仍报错,可以给插件添加明确的配置参数,指定模块名和.env文件路径:
module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      ['module:react-native-dotenv', {
        moduleName: '@env',
        path: '.env',
      }]
    ],
  }
}

内容的提问来源于stack exchange,提问作者Rogue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 16:06:00