I have two different environments with named .env.development
, .env.production
, and common .env
also.
.env.development
looks like this,
TEST_LABEL=Development
.env.production
looks like this,
TEST_LABEL=Production
.env
looks like this,
TEST_LABEL=ENV
And here is my babel.config.js
,
module.exports = function (api) {
api.cache(true);
return {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
["module:react-native-dotenv",
{
"moduleName": "@env"
},]
]
};
};
this is the scripts in package.json
"scripts": {
"start": "react-native start",
"development": "NODE_ENV=development expo start",
"production": "NODE_ENV=production expo start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "expo start --web",
"eject": "expo eject",
"test": "jest"
},
And this is how I used it in my Homescreen.js
import {TEST_LABEL} from "@env"
...
<Text>{TEST_LABEL}</Text>
And this displays always Development
even I run the production env
I run the app like npm run development
for development environment, npm run production
for production environment.
I am using react-native-dotenv
Here is the Project structure snapshot,
You can use the strings for the environment variable:
TEST_API=https://api.example.com/user/12
But, what about the strings that include the space character?
Space inside allowed only for quoted values
So, wrap the TEST_LABEL
with quotation marks ( "
or '
):
.env.demo
TEST_LABEL="Welcome to demo environment"
.env.local
TEST_LABEL="Welcome to local environment"
I found this issue and it says the only supported envs are development
, production
, and test
. If you want to use other env names you can use the experimental feature APP_ENV
"demo": "APP_ENV=demo expo start",
"local": "APP_ENV=local expo start",
and I think .local
files are loaded by default
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.