admin管理员组文章数量:1130349
I am trying to set up different environment files in a React project (using create-react-app). I following the official documentation but I am getting an error:
'.env.development' is not recognized as an internal or external mand,
I have .env, .env.local, .env.development, .env.production in the same level as package.json and src/
My scripts in package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Also tried other option: "start": ".env.local, .env.development, .env".
Both return similar errors as referenced above when I run npm start.
All my keys start with the prefix: REACT_APP_. Ex: REACT_APP_API_KEY.
What am I missing?
I am trying to set up different environment files in a React project (using create-react-app). I following the official documentation but I am getting an error:
'.env.development' is not recognized as an internal or external mand,
I have .env, .env.local, .env.development, .env.production in the same level as package.json and src/
My scripts in package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Also tried other option: "start": ".env.local, .env.development, .env".
Both return similar errors as referenced above when I run npm start.
All my keys start with the prefix: REACT_APP_. Ex: REACT_APP_API_KEY.
What am I missing?
Share Improve this question edited Apr 9, 2022 at 8:32 saomi asked Apr 7, 2022 at 12:52 saomisaomi 8955 gold badges18 silver badges41 bronze badges 1- This medium article might be worth for you to look at: dev.to/rajeshroyal/… – Awshaf Ishtiaque Commented Jun 20, 2023 at 11:51
2 Answers
Reset to default 3Here are the env files you can use and their priority (available with [email protected] and higher):
.env: Default.
.env.local: Local overrides. This file is loaded for all environments except test.
.env.development,.env.test,.env.production: Environment-specific settings.
.env.development.local,.env.test.local,.env.production.local: Local overrides of environment-specific settings.
Which means an environment variable for production goes in .env.production file, etc. And here are the steps to follow:
Create your
.env, or.env.productionfile... in the root directory of the project, same folder as wherepackage.jsonis.You define your environment variable with the prefix
REACT_APP_within those env files, like so:
REACT_APP_API_KEY=343535345235452452
- And you use them in your code this way :
- In a JavaScript file:
process.env.REACT_APP_API_KEY
- In an HTML file:
<title>%REACT_APP_API_KEY%</title>
Though if you wanna set an environment variable within the scripts, you could do it like so:
"scripts": {
"start": "REACT_APP_API_KEY=343535345235452452 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
I think you have to use the env-cmd package to use multiple env. Which is the best way to handle it. Just follow this doc for more info Multiple env example for react and env-Cmd package details
Also, please check which react/react-scripts version you are using in your project and check the version of react/react-scripts that you have created for a demo which working fine for you.
I am trying to set up different environment files in a React project (using create-react-app). I following the official documentation but I am getting an error:
'.env.development' is not recognized as an internal or external mand,
I have .env, .env.local, .env.development, .env.production in the same level as package.json and src/
My scripts in package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Also tried other option: "start": ".env.local, .env.development, .env".
Both return similar errors as referenced above when I run npm start.
All my keys start with the prefix: REACT_APP_. Ex: REACT_APP_API_KEY.
What am I missing?
I am trying to set up different environment files in a React project (using create-react-app). I following the official documentation but I am getting an error:
'.env.development' is not recognized as an internal or external mand,
I have .env, .env.local, .env.development, .env.production in the same level as package.json and src/
My scripts in package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Also tried other option: "start": ".env.local, .env.development, .env".
Both return similar errors as referenced above when I run npm start.
All my keys start with the prefix: REACT_APP_. Ex: REACT_APP_API_KEY.
What am I missing?
Share Improve this question edited Apr 9, 2022 at 8:32 saomi asked Apr 7, 2022 at 12:52 saomisaomi 8955 gold badges18 silver badges41 bronze badges 1- This medium article might be worth for you to look at: dev.to/rajeshroyal/… – Awshaf Ishtiaque Commented Jun 20, 2023 at 11:51
2 Answers
Reset to default 3Here are the env files you can use and their priority (available with [email protected] and higher):
.env: Default.
.env.local: Local overrides. This file is loaded for all environments except test.
.env.development,.env.test,.env.production: Environment-specific settings.
.env.development.local,.env.test.local,.env.production.local: Local overrides of environment-specific settings.
Which means an environment variable for production goes in .env.production file, etc. And here are the steps to follow:
Create your
.env, or.env.productionfile... in the root directory of the project, same folder as wherepackage.jsonis.You define your environment variable with the prefix
REACT_APP_within those env files, like so:
REACT_APP_API_KEY=343535345235452452
- And you use them in your code this way :
- In a JavaScript file:
process.env.REACT_APP_API_KEY
- In an HTML file:
<title>%REACT_APP_API_KEY%</title>
Though if you wanna set an environment variable within the scripts, you could do it like so:
"scripts": {
"start": "REACT_APP_API_KEY=343535345235452452 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
I think you have to use the env-cmd package to use multiple env. Which is the best way to handle it. Just follow this doc for more info Multiple env example for react and env-Cmd package details
Also, please check which react/react-scripts version you are using in your project and check the version of react/react-scripts that you have created for a demo which working fine for you.
本文标签: javascriptReactdifferent env files with createreactappStack Overflow
版权声明:本文标题:javascript - React, different env files with create-react-app - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1745128273a2135988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论