如何更新ReactJS的`create-react-app`?

1,056 阅读2分钟

我开始用npx create-react-app command 创建一个react应用程序,它给出了一个错误,你正在运行create-react-app 4.0.3,它落后于最新版本(5.0.0)。

这意味着,我的系统使用旧版本4.0.4的create-react-app,而最新版本是5.0.0。

它没有创建一个应用程序,所以我必须升级到最新版本。

这里是npx create-react-app命令的输出结果:

A:\work>npx create-react-app react-style-components
Need to install the following packages:
  create-react-app
Ok to proceed? (y) y

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

We no longer support the global installation of Create React App.

Please remove any global installs with one of the following commands:
- npm uninstall -g create-react-app
- yarn global remove create-react-app

The latest instructions for creating a new app can be found here:
https://create-react-app.dev/docs/getting-started/

为什么需要更新create-react-app?

create-react-app是一个CLI工具,可以用所有最新的react和依赖版本创建一个全新的react应用。

如果你使用的是旧版本,它将创建一个具有旧的 react-script 和依赖库(babel、webpack)版本的 react-application。

有多种方法可以将create-react-app CLI安装为最新版本:

  • 卸载并安装最新版本的create-react-app
  • 用最新的版本升级现有的应用程序。

如何将come-react-app升级到最新版本

以下是升级的步骤

首先,在终端,运行下面的命令,在全局范围内卸载come-react-app。

npm uninstall -g create-react-app

现在,你可以使用下面的命令进行全局安装

 npm install -g create-react-app

接下来,运行下面的命令来创建一个react应用程序,它可以创建一个具有所有最新版本的应用程序。

npx create-react-app appname

如何将现有的react应用程序版本升级到最新版本?

如果你有一个用create-react-app创建的旧版本的应用程序。

以下是升级最新的 react 依赖关系的步骤。

create-react-app有以下最小的依赖项

  • react-scripts 开发依赖项,为 react 应用提供启动/停止脚本
  • react
  • react-dom依赖项

进入应用程序文件夹,运行下面的命令来安装最新的版本

npm install react-scripts@latest
npm install react@latest
npm install react-dom@latest

结语

综上所述,你学会了如何将create-react-app升级到最新版本 用最新版本创建一个新的react应用 现有的react应用升级到最新版本