自从 4.0.1 版本以来,create-react-app 工具无法运行。

141
我尝试使用npm i create-react-appnpx create-react-app new-appnpm init react-app new-app安装create-react-app,但我一直收到以下错误信息:

您正在运行create-react-app 4.0.0,落后于最新版本(4.0.1)。我们不再支持全局安装Create React App。

我该如何解决?
42个回答

1

以下是对我有效的方法。

我尝试了之前回答中提到的命令。我逐个运行它们而不是将它们作为链接命令运行(以防其中任何一个失败)。但当我执行 create-react-app 命令时,它失败了。

npm uninstall -g create-react-app && npm i -g npm@latest && npm cache clean -f && npx create-react-app my-app

我不知道为什么,但即使我更新了npm并卸载了全局并清除了缓存,它也无法工作,直到我运行了

npm i create-react-app

注意:我已经安装了create-react-app,但没有使用-g标志。因为如此帖子中其他人指出的那样,全局安装create-react-app不再是推荐的做法。虽然有更好的答案,但如果有人看到这个答案,那也值得注意和尝试。

1
我已经全局卸载了create-react-app,也从yarn中卸载了它。
npm uninstall create-react-app -g

重新安装的create-react-app
npm i create-react-app

另一种方法是:
npm create-react-app@latest my-app
npm create-react-app@4.0.2 my-app

1

在浏览了Github的一些问题后,我找到了一个发布的解决方案,它不需要降级node或npm。

我正在使用WSL2上的Ubuntu,因此请注意位置可能会有所不同。问题出在npm缓存了一个全局安装的create-react-app和npx,它是v4.0.0,但当你使用npm view create-react-app时并不显示。

如果你找到自己的npm缓存,对我来说它位于~/.npm/_npx/文件夹中,你会发现所有你以前使用npx进行全局安装的哈希名称文件夹。

如果你查看每个文件夹,例如使用cat [hash]/package.json,并记录包含create-react-app的任何哈希值。现在手动编辑package.json文件,将create-react-app的版本提升到4.0.1,然后使用rm -rf [hash]/node_modules/create-react-app从节点模块中删除它。

如果您执行完上述步骤后,再次运行命令npx create-react-app my-app,它会像平常一样提示您安装 'create-react-app',然后正常工作,此时npx将缓存正确的版本,因此您不会再遇到这个问题。您可以使用任何版本的node和npm。
如果您使用的是Windows或Mac操作系统,则需要找出缓存的位置,我不知道具体在哪里,但其余步骤应该是相同的。

1
我遇到了同样的问题,但错误信息并不十分清晰。我通过更新npx本身来解决这个问题。
请在终端中输入npm update npx以解决问题。
之后,您可以输入npx create-react-app "您喜欢的项目名称"来创建您的React项目。

1
我今天也遇到了这个问题,我的根本原因可能非常罕见,但或许能帮助其他人。
我在我的 .npmrc 文件中定义了 npm 注册表,因为我们公司要求使用它。结果发现我的工作镜像没有 4.0.1 的版本,在 create-react-app 中的某些部分必须联网并运行版本检查。
如果你已经定义了注册表,请运行 npm view create-react-app 命令检查最新版本。

1

我没有全局安装create-react-app,我只是执行了npm uninstall -g create-react-app,然后执行了npm install create-react-app


1
其他答案对我没有用。例如,尝试全局卸载可能会卸载它,但错误仍然存在。它在新的Node安装中持续存在。
然而,以下方法似乎很简单,并且对我有效:
1. 进入一个新文件夹,在其中创建应用程序,以便它不知道create-react-app。 2. 运行命令yarn add create-react-app 3. 运行命令yarn run create-react-app myapp

天啊,做了“yarn add create-react-app”之后,“npx create-react-app”又可以用了!!!谢谢。 - Shai Kimchi

1

use this :

npm install create-react-app

1
在npm项目之外运行npm install create-react-app是无效的。只需运行npx create-react-app my-app即可创建一个新的React应用程序。 - ATYB

1

解决方案:

步骤1: 全局移除create-react-app包:

npm uninstall -g create-react-app

第二步:全局重新安装create-react-app软件包:

npm install -g create-react-app

这将安装版本为5.0.1(或更高版本)。要检查是否已正确安装,请在终端上运行以下命令:

npm list -g

结果:

终端上的响应

然后再次尝试使用以下命令创建React应用程序:

npx create-react-app <project_name>

然后它就能正常工作了。


0

这对我有效

npm uninstall -g create-react-app

那么

npm install -g create-react-app

2
你不需要全局安装。阅读答案 https://dev59.com/XVEG5IYBdhLWcg3wgf2Y#65043610 - Suraj Rao
它对我也起作用了,但说实话,我不知道为什么它能起作用,因为我又全局安装了它。 - Omar Zaoujal

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