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

Viewed 8268

I want to create a React app using the command

npx create-react-app my-app

But I want to make sure I can choose the React version. I want a version that can't use hooks, so I want to force a React version before 16.8 (in which hooks were released).

How or where do I find the version to use?
I have followed a tutorial that gave me a command like

npx create-react-app reactjs-app --scripts-version 1.1.5

How can I create an app with for example React version 16.7?

My goal is to get the latest version before which hooks were released.

2 Answers

According the documentation the syntax for npx is:

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

If you want react version 16.7 you have to find out which version of create-react-app installs react 16.7 because the version numbers are not the same. If I'm correct version 3.4.1 of create-react-app installs the latest version of react before they introduce hooks.

can simply run:

npx create-react-app@3.4.1 my-app 

Some packages may use a different name for the package and the tool, causing some forms of the version specifier to fail when the plain command works:

$ 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

In this case, using -p / --package, or specifying the package name instead, will allow npx to work as expected:

$ 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

(nb: the first npx uglifyjs only works when inside a project that has had uglify-js added to the package.json file.)

Related