React native-env file could not be found within the project

Viewed 1100
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";

my env file looks like this:

REACT_APP_WEATHER_API_KEY=2607fb610e6f9fecd16c84408d0b42a2

and my Babel file looks like this:

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

and my env file is in root folder along with Appjson and few other files

2 Answers

You should add a plugin like this(.env file must be in the root directory)

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

And don't forget to restart the expo start and ios/android restart the app.

If you are still getting the same issue then it can be a cache issue.

You can easily clear the babel cache by running the following command :

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

or

yarn start --reset-cache

or

expo r -c

Your babel config has 'plugin' which should be 'plugins'

Related