创建新的React应用时出错(“您正在运行` create-react-app` 4.0.3,它落后于最新版本(5.0.0)”)

68
我一直在遇到这个create React app的错误,即使卸载了也不行。 npm uninstall -g create-react-app

已更新,570ms内审核了1个软件包

未发现漏洞

npx create-react-app test-app

需要安装以下软件包:create-react-app。确定要继续吗?(y) y

您正在运行的是create-react-app 4.0.3版本,比最新版本(5.0.0)要旧。

我们不再支持全局安装Create React App。

请使用以下命令之一删除任何全局安装:

  • npm uninstall -g create-react-app
  • yarn global remove create-react-app

创建新应用程序的最新说明可以在此处找到: https://create-react-app.dev/docs/getting-started/

C:\>npm --version
8.3.0
C:\>node --version
v16.13.0

我该如何修复这个问题?

22个回答

170

尝试运行 npx clear-npx-cache 命令来清除你的 npx 缓存,然后再运行 npx create-react-app 你的应用名 命令。

还可以参考 这个回答 来清除缓存。

此外,也可以尝试强制使用最新版本:

npx create-react-app@latest my-app --use-npm


你的第二个命令有效,但我不明白,为什么会出现这个错误? - vishal
在我的情况下,在运行“npx create-react-app”之后,终端中出现了有关npm新主要版本的通知。我使用“npm install -g npm@x.x.x”(需要将x替换为通知中显示的版本)。之后我再次运行“npx create-react-app”,它就可以正常工作了。 - Mohamed Saheed Mohamed Riswan

21
我已经采取了以下步骤解决此问题:
  1. npm uninstall -g create-react-app

  2. npx clear-npx-cache

  3. npx create-react-app myapp

这些步骤可以帮助您重新安装create-react-app。

12

解决方案1:强制更新到最新版本

尝试使用force命令将其强制更新到最新版本,像这样:

npx create-react-app@latest_version my-app --use-npm

最新版本是5.0.0,所以请尝试这个命令:just try

npx create-react-app@5.0.0 my-app

现在,您的错误必须得到解决。

解决方案2:清除缓存

您只需要清除缓存,然后就可以创建一个新的React项目。首先,通过运行此命令来清除缓存:

npx clear-npx-cache

如果它不起作用,尝试使用此方法并重试:删除此路径下的所有内容。

C:\Users\Your_user_name\AppData\Roaming\npm-cache
在 macOS(以及可能的 Linux)上,它是 ~/.npm/_npx。你可以使用以下命令删除它:

在 macOS(以及可能的 Linux)上,它是 ~/.npm/_npx。你可以使用以下命令删除它:

rm -rf ~/.npm/_npx

或者尝试在终端中使用管理员模式运行命令npm cache clean --force。现在,只需重新运行创建命令。

npx create-react-app your-app

解决方案3:运行以下命令

请逐个运行以下命令。

  1. npm uninstall -g create-react-app
  2. npm cache clean --force
  3. npm cache verify
  4. npx create-react-app my-app

1
对我来说,这个问题在使用 create-react-app@latest 时解决了,但是在使用 create-react-app@latest_version 时没有解决。 - user56reinstatemonica8

4

你需要清除npx缓存才能使其正常工作。

你可以使用npm ls -g create-react-app命令查找create-react-app所安装的文件夹位置。

另外,要清除缓存,请参考此答案:如何清除`npx`的中央缓存?


2
感谢您的评论,Sawan。是的,清除缓存确实解决了我的问题。 - Newbie learner

3

我通过运行解决了这个问题

$ npm uninstall -g create-react-app

那么
$ npx clear-npx-cache

那么。
$ npm install -g create-react-app@5.0.0

最后,它终于能够使用:

$ npx create-react-app my-app

3

我只需要运行npx create-react-app@latest my-app-name即可完成。


2

(npx create-react-app@5.0.0 my-app) 运行得很顺利,而且比旧版本的React JS要快得多。


2
我遵循以上建议但仍未解决我的问题。 以下步骤解决了我的问题:
1- 了解npm缓存文件夹路径:
npm cache verify
这会给你npm缓存文件夹的路径: Cache verified and compressed (~\AppData\Local\npm-cache_cacache)
2- 进入".. \ npm-cache" 文件夹并删除所有内容。
3- 创建您的项目:
npx create-react-app my-app

2
我尝试了这两个命令,它们都可以正常工作。你可以任选其中一个进行尝试。 npx create-react-app@5.0.0 my-app 或者 npx clear-npx-cache

1

我曾遇到同样的问题,通过运行以下命令解决了它:

npx clear-npx-cache
npm cache verify

进入之前使用的命令获取路径的“..\npm-cache”文件夹并删除其中所有内容。

npm install -g npm@8.3.0
npx create-react-app your_app_name

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