HOST未被识别为内部或外部命令

6

在克隆了一个包含ReactJS和NodeJS项目的Git仓库之后,安装NodeJS

NodeJS installed

我希望在浏览器中看到结果,因此必须在根目录下运行,该目录包含一个 package.json 文件。

npm install

然后执行以下命令:

npm start

第一个命令正常工作,但是第二个命令出现以下错误:

> 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:

package.json文件具有以下内容

"scripts": {
  "start": "HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start",

我使用Git Bash、CMD和PowerShell进行测试,但一直收到相同的错误,如下图所示:

Git Bash

Git Bash HOST is not recognized as an internal or external command

CMD CMD HOST未被识别为内部或外部命令

PowerShell PowerShell HOST未被识别为内部或外部命令


@Quentin 是的,我也认为它会起作用,但事实并非如此... - Tiago Martins Peres
@Quentin刚刚添加了图片来证明我所说的话。 - Tiago Martins Peres
它按预期打开(使用Git Bash) - Tiago Martins Peres
1
可能与 npm 如何调用外部程序有关。 - Quentin
1
发现重复项。请使用 cross-env 包。 - Quentin
显示剩余2条评论
1个回答

12

方法一

如果我运行以下命令(而不使用npm包装脚本):

HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start

启动开发服务器...

运行良好,正如Quentin所说的那样,

一定是与npm如何外壳相关的问题。

为了修复它,我已经转到package.json并将"start"脚本更改为

"start": "./node_modules/.bin/react-scripts start",

然后npm start就可以正常工作了。


方法2

使用cross-env包。

为此,请使用以下命令安装:

npm i cross-env

然后转到 package.json 并将其更改为

"start": "cross-env ./node_modules/.bin/react-scripts start",

然后运行npm start也可以正常工作:

cross-env npm start


1
使用 cross-env 可以正常工作。 - Srinidhi S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接