自从 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个回答

238
这对我有用:
npx create-react-app@latest your-project-name --use-npm

148
根据create-react-app文档的说法,不应该全局安装create-react-app
如果您之前通过npm install -g create-react-app全局安装了create-react-app,我们建议您使用npm uninstall -g create-react-appyarn global remove create-react-app卸载该包,以确保npx始终使用最新版本。
这甚至在您收到的错误消息中也有说明:
您正在运行create-react-app 4.0.0,该版本落后于最新发布的版本(4.0.1)。 我们不再支持全局安装Create React App。 您必须使用npm uninstall -g create-react-app卸载create-react-app
然后,每次您想要使用create-react-app创建新的React应用程序时,请使用命令npx create-react-app my-app

要解决您遇到的错误,首先全局卸载create-react-app,然后更新npm,清除缓存,最后重新尝试创建应用程序。

在终端中运行以下命令:

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

7
“清除缓存”是我之前遗漏的重要步骤,谢谢。 - Brandon Johnson

34

我明白了

You are running `create-react-app` 4.0.3, which is behind the latest release (5.0.0).

We no longer support global installation of Create React App.

所以,我只是使用了显式版本调用了这个包:

npx create-react-app@5.0.0 app-name

如果在全局卸载并清除缓存后仍然遇到该问题,请尝试运行 npm uninstall create-react-app,因为您可能安装了非全局的版本。 - Stephen Fong
在全局卸载并清除缓存后,使用显式版本解决了我的问题。 - Leonardo Viada

27

在发布v4.0.2后,我也遇到了这个问题。他们提到了以下内容:

如果您之前通过 npm install -g create-react-app 全局安装了 create-react-app,我们建议您使用 npm uninstall -g create-react-appyarn global remove create-react-app 来卸载该软件包,以确保 npx 始终使用最新版本。

我按照以下步骤解决了这个问题:

  1. 卸载 create-react-app v4.0.1:

    # for npm:
    npm uninstall -g create-react-app
    
    # for yarn:
    yarn global remove create-react-app
    
    你不需要在本地目录中安装create-react-app,所以如果你不想这样做,请跳至第3步。如果你想这样做,请使用以下命令安装v4.0.2不要使用全局标志(-g--global):
    # for npm:
    npm i create-react-app
    
    # for yarn:
    yarn add create-react-app
    
    现在您可以使用以下命令创建新的React应用程序:
  2. 您现在可以使用以下命令创建新的React应用程序:

    # for npx:
    npx create-react-app my-app
    
    # for npm:
    npm init react-app my-app
    
    # for yarn:
    yarn create react-app my-app
    

这个可以运行...需要注意的是,如果你因为不同的原因同时使用npm和yarn,你可能会遇到这样一种情况:尝试使用错误的包管理器(而不是你安装它的那个)卸载create-react-app。在这种情况下,请尝试使用npm和yarn卸载命令...其中一个应该可以工作。 - Jo Momma

16

我也遇到了同样的问题,但当我全局卸载 create-react-app 并再次全局安装后,问题得以解决。

卸载命令:

npm uninstall -g create-react-app

安装命令:

npx create-react-app my-app

如果你使用的是较旧的npm版本 (npm version < 5.2),则使用此命令:

npm install -g create-react-app

这解决了我的问题,希望它也能解决您的问题。


5
这个答案是错误的:create-react-app 不应该全局安装 - ATYB

10

更新 NPX 对我有用。本页面的建议没有起到作用,但可能有所帮助。

npm update npx

2
这是唯一对我有效的方法!令人惊讶的是,更新/重新安装软件包可以解决多少错误。 - Matt Croak

9
 npm uninstall -g create-react-app

虽然卸载命令成功运行,但它无法卸载create-react-app,所以我一遍又一遍地遇到相同的错误。

这最终对我起作用了 npx create-react-app@latest my-app --template typescript


1
对我来说,卸载并没有起作用,是@latest让它开始运行的。谢谢 :) - J4v4Scr1pt

8

我的做法是:

npm uninstall -g create-react-app 

接下来:

npm i create-react-app # or yarn add create-react-app

我还更新了Node版本。


8

使用npm全局卸载create-react-app并去除全局标志后重新安装对我来说没有起作用。

npm uninstall -g create-react-app
npm install create-react-app

我原本使用的是 node 版本 15.2.0,现在通过 nvm 升级到了最新稳定版本 15.3.0。

nvm install node

然后我再次安装了create-react-app(没有使用全局标志)。

npm install create-react-app

我随后成功创建了一个新的React应用程序。

npx create-react-app my-app

编辑:以上命令将会在当前目录下安装create-react-app。需要明确的是,在全局卸载create-react-app之后,我无法再通过npx创建应用程序并收到相同的错误信息。目前我找到的最佳解决方案是运行npm install -g npm@latest,将我的npm版本从7.0.14开发版降级到6.14.9 LTS构建,并使我能够通过npx轻松创建React应用程序。


在 npm 项目之外运行 npm install create-react-app 不会有任何作用。只需运行 npx create-react-app my-app 就可以创建一个新的 React 应用程序。 - ATYB
@ΛRYΛN 在卸载全局后运行“npx create-react-app my-app”时,与原帖相同地出现了错误。这并没有起作用。在没有全局标志的情况下安装create-react-app会将其安装到我的当前目录,这使我能够创建一个新的react应用程序,但您是正确的,也许这不是最好的方法。我已经找到了更好的方法,并更新了我的帖子,但我仍在努力弄清楚create-react-app究竟安装在哪里,如果不是全局的话。我在任何预期的位置中都没有看到它,主要是 /Users/{username}/.nvm/versions/node/v15.3.0/lib/node_modules/npm/node_modules。 - Alex Hall

6

在使用 create-react-app > 4.0.1 后,请使用以下命令:

npm

npm init react-app my-app

yarn

yarn create react-app my-app

入门 | Create React App 文档


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