【问题标题】:React native-env file could not be found within the project在项目中找不到 React native-env 文件
【发布时间】:2021-09-14 10:17:36
【问题描述】:
Unable to resolve module @env from D:\react\weatho\weatho-app\App.js: @env could not be found within the project

10:import {REACT_APP_WEATHER_API_KEY} from "@env";

我的 env 文件如下所示:

REACT_APP_WEATHER_API_KEY=2607fb610e6f9fecd16c84408d0b42a2

我的 Babel 文件如下所示:

  module.exports = function(api) {
        api.cache(true);
        return {
        plugin: ['babel-preset-expo','module:react-native-dotenv'],
     }
   }

我的 env 文件与 Appjson 和其他一些文件一起位于根文件夹中

【问题讨论】:

    标签: reactjs environment-variables native


    【解决方案1】:

    你的 babel 配置有 'plugin' 应该是 'plugins'

    【讨论】:

      【解决方案2】:

      你应该像这样添加一个插件(.env文件必须在根目录下)

      module.exports = function (api) {
      api.cache(true);
      return {
      presets: ["babel-preset-expo"],
      plugins: [
        [
          "module:react-native-dotenv",
          {
            moduleName: "@env",
            path: ".env",
          },
        ],
      ],
      };
      };
      

      别忘了重启 expo start 和 ios/android 重启应用。

      如果您仍然遇到同样的问题,则可能是缓存问题。

      您可以通过运行以下命令轻松清除 babel 缓存:

      rm -rf node_modules/.cache/babel-loader/*
      

      yarn start --reset-cache
      

      expo r -c
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 2020-02-17
        • 2018-09-09
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        • 2016-12-31
        相关资源
        最近更新 更多