首页 > 解决方案 > React 变量 REACT_APP_API_HOST 不是内部或外部命令

问题描述

我是 React 项目的初学者并尝试使用运行项目,yarn start但出现此错误:

D:\Projects\Demo>yarn start
yarn run v1.7.0
$ REACT_APP_API_HOST=https://us-central1-mydemo.cloudfunctions.net/ react-s
cripts start
'REACT_APP_API_HOST' is not recognized as an internal or external command,
operable program or batch file.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

这就是 package.json 中的内容:

"scripts": {
    "start": "REACT_APP_API_HOST=https://us-central1-mydemo.cloudfunctions.net/ react-scripts start"
}

如何修复此错误?任何帮助表示赞赏。

标签: node.jsreactjs

解决方案


如果您使用的是 Windows,则在环境变量方面可能会令人窒息。我建议获取跨环境包(https://www.npmjs.com/package/cross-env)并将您的启动脚本更改为:

"scripts": {
    "start": "cross-env REACT_APP_API_HOST=https://us-central1-mydemo.cloudfunctions.net/ react-scripts start"
}

推荐阅读