如何在 NPM 启动脚本中设置 React 应用程序变量

How to set React app variables in NPM start script

所以我的 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\_logs19-09-22T15_12_59_217Z-debug.log

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

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

npmjs.com/package/dotenv