Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@env couldn't found #5

Open
sumit96367 opened this issue Apr 3, 2023 · 5 comments
Open

@env couldn't found #5

sumit96367 opened this issue Apr 3, 2023 · 5 comments

Comments

@sumit96367
Copy link

@env could not be found within the project or in these directories:
node_modules
1 | import { useState, useEffect } from "react";
2 | import axios from "axios";

3 | import { RAPID_API_KEY } from '@env'
| ^
4 |
5 | const rapidApiKey = RAPID_API_KEY;

@JPaul23
Copy link

JPaul23 commented Apr 12, 2023

Hello @sumit96367 , after some research this is how I resolved it on my end
In my useFetch.js

import { RAPID_API_KEY } from '@env';
const rapidApiKey = RAPID_API_KEY;

and in my babel.config.js I added this ["module:react-native-dotenv"] in plugin as below:

plugins: [
      "@babel/plugin-proposal-export-namespace-from",
      "react-native-reanimated/plugin",
      require.resolve("expo-router/babel"),
      ["module:react-native-dotenv"],
    ],

Let me know if it works for you.

@JatinBisht2308
Copy link

JatinBisht2308 commented Apr 29, 2023

@JPaul23 @sumit96367 @adrianhajdin
After that, it will give and error like react-native-dotenv module not found so download the react-native-dotenv module.
npm install react-native-dotenv
Then it will work.

@AlejandroSuero
Copy link

AlejandroSuero commented Sep 5, 2023

Hello @sumit96367 and everyone else who has this problem,

I've started with react native through JavaScriptMastery this week and I've been getting the same error. And here is my solution.

I added this to my app.json, which I renamed as app.config.js

// app.config.js
import "dotenv/config"

export default {
    // Default app.json
    extra: {
        // Default extra
        rapidApiKey: process.env.RAPID_API_KEY,
        // ...
    }
}

If you don't have dotenv installed:

npm install --save-dev dotenv

Then to call these enviroment variables:

// useFetch.js
// ... imports
import Constants from "expo-constants"

const rapidApiKey = Constants.expoConfig.extra.rapidApiKey
// ...

If you don't have expo-constants installed:

npx expo install expo-constants

@adewale2018
Copy link

Hello @sumit96367 and everyone else who has this problem,

I've started with react native through JavaScriptMastery this week and I've been getting the same error. And here is my solution.

I added this to my app.json, which I renamed as app.config.js

// app.config.js
import "dotenv/config"

export default {
    // Default app.json
    extra: {
        // Default extra
        rapidApiKey: process.env.RAPID_API_KEY,
        // ...
    }
}

If you don't have dotenv installed:

npm install --save-dev dotenv

Then to call these enviroment variables:

// useFetch.js
// ... imports
import Constants from "expo-constants"

const rapidApiKey = Constants.expoConfig.extra.rapidApiKey
// ...

If you don't have expo-constants installed:

npx expo install expo-constants

Thank you for this solution. This is my first time of learning react-native.

@JohnnyZhov
Copy link

JohnnyZhov commented Mar 13, 2024

Hello coders!

I found solution the next way:

  1. Install the following packages:
   npx expo install expo-constants expo-config
   npm install --save-dev metro-react-native-babel-preset
  1. Rename 'app.json' to 'app.config.js':
const extra = require('./extra');

module.exports = {
  expo: {
    name: "react_native_jobs",
    slug: "react_native_jobs",
    version: "1.0.0",
    orientation: "portrait",
    icon: "./assets/images/icon.png",
    scheme: "myapp",
    userInterfaceStyle: "automatic",
    splash: {
      image: "./assets/images/splash.png",
      resizeMode: "contain",
      backgroundColor: "#ffffff"
    },
    assetBundlePatterns: [
      "**/*"
    ],
    ios: {
      supportsTablet: true
    },
    android: {
      adaptiveIcon: {
        foregroundImage: "./assets/images/adaptive-icon.png",
        backgroundColor: "#ffffff"
      }
    },
    web: {
      bundler: "metro",
      output: "static",
      favicon: "./assets/images/favicon.png"
    },
    plugins: [
      "expo-router"
    ],
    experiments: {
      typedRoutes: true
    },
    extra: extra,
  }
};
  1. Create a file named 'extra.js' in the root directory:
module.exports = {
  RAPID_API_KEY: 'rapid_api_key',
};
  1. Your 'Babel.config.js':
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ['module:react-native-dotenv']
  ],
};
  1. Use the following approach in 'useFetch.js':

    import Constants from 'expo-constants';
    // This extracts the RAPID_API_KEY from our Expo configuration. This is the API key that we're using to authenticate our requests.
    const { RAPID_API_KEY } = Constants.expoConfig; 
    // This creates a new variable rapidApiKey and assigns it the value of RAPID_API_KEY. This step is not strictly necessary, as you could use RAPID_API_KEY directly. However, it can make your code clearer by giving a more descriptive name to the API key.
    const rapidApiKey = RAPID_API_KEY; 

Hopefully, the original set-up works for the majority. If not, this approach could be a solution for someone who runs into the same problem as I did.

Happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants