create-react-app local和dev环境变量

Ale*_*tzl 3 environment-variables node.js reactjs create-react-app

我有.env.local

REACT_APP_BACKEND_BASEURL=http://localhost:8080/
Run Code Online (Sandbox Code Playgroud)

.env.development

REACT_APP_BACKEND_BASEURL=http://deployedserverurl:8080/
Run Code Online (Sandbox Code Playgroud)

如何在启动时选择正确的env文件?就像现在一样,它更喜欢本地的dev env文件.

npm start --env=local 似乎不起作用,我错过了什么?

Ale*_*x K 6

根据当前环境导入环境变量.有一个带有create-react-app被调用的内置特殊环境变量NODE_ENV.总之,当您运行变量时,npm startNODE_ENV变量设置为development并在运行时设置为production npm run build.因此,如果.env.development在项目的根目录中创建a ,则在运行npm start这些变量时,将在环境中搜索定义.

另外,请确保使用正确使用它们process.env.REACT_APP_APP_BACKEND_BASEURL.

如果您需要有关不同类型.env文件的所有详细信息的更多信息,请从React Docs中查看以下内容:

env:默认.

.env.local:本地覆盖.除test之外的所有环境都加载此文件.

.env.development,.env.test,.env.production:特定环境的设置.

.env.development.local,.env.test.local,.env.production.local:特定环境设置本地覆盖.