简体   繁体   English

Override.env 与内联 arguments

[英]Override .env with inline arguments

Is it possible to override the variables inside.env on run or build?是否可以在运行或构建时覆盖 inside.env 中的变量?

Content of the.env file .env 文件的内容

REACT_APP_REGIONALIZED_BASE_API_URL=http://hello-default.net

Command script in package.json package.json中的命令脚本

"scripts": {
  "start:production": "env-cmd -f .env react-scripts start",
}

Sample start command executed in terminal or even in ci/cd pipeline where you have instances based on regions在终端或什至在 ci/cd 管道中执行的示例启动命令,您有基于区域的实例

REACT_APP_REGIONALIZED_BASE_API_URL=http://hello-eu.net yarn start:production

So, instead of having a build with api url http://hello-default.net , it should have http://hello-eu.net因此,与其使用 api url http://hello-default.net进行构建,不如使用http://hello-eu.net

Sharing the [alternative] solution I discovered.分享我发现的 [alternative] 解决方案。 Just need to add --no-override argument.只需要添加--no-override参数。 This will prevent replacing of the already set variable.这将防止替换已经设置的变量。 Also applicable with multiple env files.也适用于多个 env 文件。

"scripts": {
  "start:production": "env-cmd --no-override -f .env react-scripts start",
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM