如何使用 npx 命令创建具有特定版本的 React 应用程序?

How create a react app with a specific version using the npx command?

我想使用命令创建一个 React 应用程序

npx create-react-app my-app

但我想确保我可以选择 React 版本。 我想要一个不能使用钩子的版本,所以我想强制一个React 16.8 之前的版本(其中发布了钩子)。

如何或在哪里找到要使用的版本?
我遵循了一个教程,该教程给了我一个像

这样的命令
npx create-react-app reactjs-app --scripts-version 1.1.5

我如何使用 React 版本 16.7 创建应用程序?

我的目标是获取发布挂钩之前的最新版本。

根据文档,npx 的语法是:

npx [options] <command>[@version] [command-arg]...

如果你想要 React 版本 16.7 你必须找出哪个版本的 create-react-app 安装 react 16.7 因为版本号不一样。如果我是正确的,create-react-app 的 3.4.1 版本会在引入 hooks 之前安装最新版本的 react。

可以简单地 运行:

npx create-react-app@3.4.1 my-app 

一些包可能对包和工具使用不同的名称,导致某些形式的版本说明符在普通命令工作时失败:

$ npx uglifyjs --version
uglify-js 3.14.4

$ npx uglifyjs@3.10.0 --version
npm ERR! code ETARGET
npm ERR! notarget No matching version found for uglifyjs@3.10.0.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
[..]
Install for [ 'uglifyjs@3.10.0' ] failed with code 1

在这种情况下,使用 -p / --package 或改为指定包名称,将允许 npx 按预期工作:

$ npx --package uglify-js@3.10.0 uglifyjs --version
npx: installed 1 in 0.631s
uglify-js 3.10.0

$ npx uglify-js@3.10.0 --version
npx: installed 1 in 0.524s
uglify-js 3.10.0
undefined

(注意:第一个 npx uglifyjs 仅在已将 uglify-js 添加到 package.json 文件的项目中有效。)