【问题标题】:define .env file except default env file in react app在 React 应用程序中定义除默认 env 文件外的 .env 文件
【发布时间】:2019-09-10 08:15:57
【问题描述】:

我需要为每个开发/生产服务器加载不同的 .env 文件。

localhost           | .env.localhost
development         | .env.development
production          | .env.production

基于 react 文档,默认我们可以使用 .env.development 和 .env.production

https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables

但我想使用新的环境变量作为本地主机。我怎样才能做到这一点?我想通过 .env.localhost 变量的文件运行我的脚本之一。

【问题讨论】:

  • 谢谢,但我想为我的一个脚本的开发环境设置不同的变量,除了启动具有我们在 .env.development 中设置此环境变量的开发环境的脚本。
  • 你需要使用.env.localhost吗?因为 .env.local 支持使用 create-react-app 开箱即用。
  • 是 localhost 或例如 .env.staging

标签: reactjs environment-variables create-react-app


【解决方案1】:

您可以从以下位置修改 package.json 的脚本部分:

"start": "react-scripts start"

对于 Linux 和 MacOS:

"start": "PORT=3006 react-scripts start"

Windows 到:

"start": "set PORT=3006 && react-scripts start"

如果你想在环境中设置新变量。然后在 shell 的 .env 文件中添加 then,但名称以 REACT_APP 开头

REACT_APP_MY_VAR

【讨论】:

  • 我想在我的一个脚本中从文件名 .env.localhost 加载环境变量,而不是你说亲爱的 Rajender。
  • 那我想,你需要这个包npmjs.com/package/react-app-env
猜你喜欢
  • 1970-01-01
  • 2020-07-04
  • 2021-12-14
  • 1970-01-01
  • 2021-02-27
  • 2022-01-11
  • 2021-06-02
  • 1970-01-01
  • 2021-11-07
相关资源
最近更新 更多