0

I have a React Native application and for some secrets I use react-native-dotenv. How can I put a variable from my .env in a script from my package.json?

For example: "myScript": "run my command with ${myEnvVariable}"

4
  • Depends what exactly your script does - see e.g. npmjs.com/package/dotenv#user-content-preload.
    – jonrsharpe
    Commented Feb 11, 2022 at 16:55
  • thx @jonrsharpe ok but it's not really dotenv package but react-native-dotenv
    – E.D
    Commented Feb 11, 2022 at 17:06
  • Which uses dotenv.
    – jonrsharpe
    Commented Feb 11, 2022 at 17:08
  • ok thx again for your response
    – E.D
    Commented Feb 11, 2022 at 17:15

1 Answer 1

1

react-native-dotenv maintainer here. This library is a babel plugin so it will use environment variables set in your script to compile your Javascript code. However, I'll do my best to answer the question. If you're looking to set environment variables in your script it depends on your operating system.

For unix systems (MacOS / Linux):

"start:development": "NODE_ENV=development MY_VAR=hello npx react-native start",

For Windows Powershell:

"start:development": "Set MY_VAR=hello&& npx react-native start",

The library will not be able to get any values until after the script variables are set. You will have to obtain the environment variables in the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.