如何将 .env.qa 或 .env.staging 与 create react app 一起使用

cyr*_*yrf 4 reactjs create-react-app dotenv env-cmd

CreateReactApp 文档显示您可以使用有限数量的 .env 文件。 https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

我想在其他环境中使用更多 .env 文件,但.env.development总是被使用。

如何使用其他文件?我找到了这篇文章,但它对我不起作用。 https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d

我正在使用react-scriptsv3.4.1

cyr*_*yrf 10

Well, the article is actually right. I had a bug in my code, where I had hard-coded the value rather than relying on process.env.REACT_APP_SOMEVAR

These scripts in package.json work fine:

"start": "REACT_APP_ENV=dev npm run start-env",
"start-env": "sh -ac '. .env.${REACT_APP_ENV}; react-scripts start'",
"start-dev": "REACT_APP_ENV=dev npm run start-env",
"start-qa": "REACT_APP_ENV=qa npm run start-env",
"build": "REACT_APP_ENV=dev npm run build-env",
"build-env": "sh -ac '. .env.${REACT_APP_ENV}; react-scripts build'",
"build-dev": "REACT_APP_ENV=dev npm run build-env",
"build-qa": "REACT_APP_ENV=qa npm run build-env",
Run Code Online (Sandbox Code Playgroud)