【问题标题】:How to set React app variables in NPM start script如何在 NPM 启动脚本中设置 React 应用程序变量
【发布时间】:2020-01-22 19:50:11
【问题描述】:

所以我的 package.json 中的 npm start 看起来像这样

"start": "REACT_APP_Environment=development react-scripts start",

这对于该项目的以前的开发人员来说效果很好,可能是因为他们在 MacOS 上,这对我来说在 Windows 上不是这样,当我运行时我得到了

> REACT_APP_Environment=development react-scripts start

'REACT_APP_Environment' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! enterprise@0.1.0 start: `REACT_APP_Environment=development react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the enterprise@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Omar\AppData\Roaming\npm-cache\_logs\2019-09-22T15_12_59_217Z-debug.log

我可以通过从启动脚本中删除环境变量并通过命令set "REACT_APP_Environment=development" && npm start 运行来解决此问题,但这并不方便,我怎样才能将它全部包含在内并在我的启动脚本中的 Windows 上正常工作?谢谢

【问题讨论】:

标签: reactjs npm npm-scripts


【解决方案1】:

我建议检查 .env,似乎它可以解决您的问题

npmjs.com/package/dotenv

【讨论】:

  • 谢谢,但是我不想添加更多的包,是否可以以某种方式修改启动脚本?
  • 如果您愿意,您可以编写一个与 .env 执行相同操作的基本脚本。但实际上,dotenv 没有依赖关系,它超级基础而且非常方便,所以你只需添加这个小包就很棒了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2021-11-12
  • 2012-09-18
  • 2018-02-23
  • 2018-10-29
  • 1970-01-01
相关资源
最近更新 更多