使用 ReactJS 和 NodeJS 项目克隆 Git 存储库并安装NodeJS 后
想在浏览器中查看结果,因此必须在具有 package.json 的根文件夹中运行
安装
之后
启动
第一个命令工作正常,但第二个命令出现以下错误
> HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start
'HOST' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ...: `HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ... 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:
Run Code Online (Sandbox Code Playgroud)
package.json 有以下内容
"scripts": {
"start": "HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start",
Run Code Online (Sandbox Code Playgroud)
使用 Git Bash、CMD 和 PowerShell 进行测试并不断得到相同的错误,如下图所示
git bash
如果我运行(不使用 npm 包装脚本)
HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start
Run Code Online (Sandbox Code Playgroud)
它工作正常。正如昆汀所说,
一定与 npm 的 shell 方式有关
为了修复它,我去了 package.json 并将“开始”脚本更改为
"start": "./node_modules/.bin/react-scripts start",
Run Code Online (Sandbox Code Playgroud)
然后npm start工作正常。
使用跨环境包。
为此,使用以下命令安装它
npm i cross-env
Run Code Online (Sandbox Code Playgroud)
然后转到 package.json 并将其更改为
"start": "cross-env ./node_modules/.bin/react-scripts start",
Run Code Online (Sandbox Code Playgroud)
然后运行npm start也可以正常工作:
| 归档时间: |
|
| 查看次数: |
5525 次 |
| 最近记录: |