How to install react (v16.x) and react-scripts (3.x) - reactjs

As per the title, I don't want to use
npx create-react-app app-name
, because that command installs react (17x) and react-scripts (4.x)
I tried
npm init react-app app-name --scripts-version 3.4.4
, but even though it installs react-scripts (v3.4.4), it still installs react (17.x)
Also, this gives another error:
error : Cannot find module 'cra-template'
EDIT: To clarify, I want to use CRA for sure.
Just not with the current versions of react-17, react-dom-17, and react-scripts-4
I also don't want to waste time installing v17.x, and v4.x, delete them manually, modify package.json to the versions I want, and npm (re-)install. That will work, but it's not the point.

npm init react-app appname --scripts-version <XX.XX.XX>
or sudo npm init react-app appname --scripts-version <XX.XX.XX>
The version number <XX.XX.XX> is for react-scripts to generate a specific react-app based on dependent version of the initiated react-scripts module.
You will have to do some legwork to see which react-scripts version relates to the specific react version when initializing a building of a new react-app.
React Versions: https://reactjs.org/versions/
React Scripts Versions: https://www.npmjs.com/package/react-scripts
If it keeps installing the newest React version
Enter npm uninstall react,
Then npm install react#XX.XX.XX
Where XX.XX.XX is your target number
You will have to do the same for react-dom (same ver# as react) and react-scripts.
Also the #testing-library dependencies may also need uninstalling (There should be 3 on instantiation - they will not work with previous versions (pre-v17 React))
I came across this exact same problem today, and that was my solution.

Related

How to update the create react app to its latest version?

I am trying to update the create-react-app to its latest version. In the docs, they have mentioned using the below command to update the react scripts to 5.0.0.
sudo npm i create-react-app#5.0.0
I wonder if there is any way to update create-react-app to its latest stable released version without knowing the exact version.
Updating your an existing project build tooling is typically a daunting and time-consuming task. When new versions of Create React App are released, you can upgrade using a single command:
npm install react-scripts#latest
In most cases bumping the react-scripts version in package.json and running npm install (or yarn install) should be enough.
When you run npx create-react-app my-app it automatically installs the latest version of Create React App.
Create React App creates the project with the latest version of react-scripts so you’ll get all the new features and improvements in newly created apps automatically.
If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.
Reference: https://create-react-app.dev/docs/updating-to-new-releases

Why can't I start a React project with npx?

I'm going in circles with npx trying to start a new project. Here's what I get:
"PS C:\Users\John\Documents\WebSites\react_projects> npx create-react-app material-ui
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 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/"
When I type npm uninstall -g create-react-app, I get "up to date, audited 1 package in 495ms. found 0 vulnerabilities."
So I try to create the app again with npx, and I get this:
"Need to install the following packages:
create-react-app
Ok to proceed? (y)"
So I press y, and I get the first error message again. I just go in circles. How can I fix this?
Refer to official reactjs website you need to have ""Node >= 14.0.0 and npm >= 5.6"" on your machine. https://reactjs.org/docs/create-a-new-react-app.html .
Also yarn create is available in Yarn 0.25+
so you might want to check these versions
npm
stands for node package manager. It helps to install, migrate apps by adding dependencies in package.json file. You only need to run npm i. For example in react API call npm i axios you should run and import in file in order to use axios features. Also all you need to have is package.json following dependencies and packages will be added(except node_modules folder as it comes with packages and more in size).
You can use npm to start, build, eject, watch and test.
npx is a CLI tool which provide way to install and manage dependencies hosted in npm registry. It is bundled with npm since 5.2.0v. It is helpful in installing and getting started with frontend JS libraries like reactjs, angularjs, vuejs, Sveltejs or boilerplate apps.

Could not find a required file. - Adding TypeScript to React project

Trying to add TypeScript to an already created React app via: https://create-react-app.dev/docs/adding-typescript
I do everything bu get the following error when I try and start:
> npx react-scripts start
Could not find a required file.
Name: index.js
Searched in: /home/karl/dev/afry/tmr-client/src
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! tmr-client#0.1.0 start: `npx react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the tmr-client#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/karl/.npm/_logs/2020-11-06T18_43_02_622Z-debug.log
I renamed the index.js to index.ts as stated in the guide.
I faced the same issue today with create-react-app. The problem is the version of react-scripts as per this Git issue: Git Reference
A quick solution is to update react-scripts. I am using react-scripts version 4.0.3 with no issues.
A safe way to do it based on the documentation: Create React App Docs would be to change the version of react-scripts in the package.json file to the most recent stable version like "react-scripts": "^4.0.3". Then, run npm install from the command line.
TLDR: npm i -D react-scripts#latest
Outdated react-scripts Version
I'm not sure why index.ts isn't working for you, it seems to be supported.
The only explanation is that you're using an outdated version of react-scripts, which you can update by simply running npm i -D react-scripts#latest. More detail from Updating to New Releases:
Create React App is divided into two packages:
create-react-app is a global command-line utility that you use to
create new projects. react-scripts is a development dependency in the
generated projects (including this one). When you run npx
create-react-app my-app it automatically installs the latest version
of Create React App.
If you've previously installed create-react-app globally via npm
install -g create-react-app, please visit Getting Started to learn
about current installation steps.
Create React App creates the project with the latest version of
react-scripts so you’ll get all the new features and improvements in
newly created apps automatically.
To update an existing project to a new version of react-scripts, open
the changelog, find the version you’re currently on (check
package.json in this folder if you’re not sure), and apply the
migration instructions for the newer versions.
In most cases bumping the react-scripts version in package.json and
running npm install (or yarn install) in this folder should be enough,
but it’s good to consult the changelog for potential breaking changes.
We commit to keeping the breaking changes minimal so you can upgrade
react-scripts painlessly.
If this is the issue, you're relying on global installation of create-react-app - which is not recommended given that it won't use the latest version. See note under Quick Start:
If you've previously installed create-react-app globally via npm
install -g create-react-app, we recommend you uninstall the package
using npm uninstall -g create-react-app or yarn global remove
create-react-app to ensure that npx always uses the latest version.

How to have create-react version 2 above?

I'm following one of the tutorial for reactjs. Now one of the topic is to check the version of my react-script in the package.json file. When I checked it's in version 0.9.5 but the instructor's copy is in 1.0+ now I want to upgrade my version my running npm install react-script#latest now it successfully updated to 2.0.5 but when I tried to create a new app using create-react-app myproject it's still using the 0.9.5 how can I install a new app with the latest version of react-scripts?
You are updating the local version of create-react-app but then you're using the global one.
You should follow the official documentation about this: https://create-react-app.dev/docs/getting-started
The main points are:
Ensure you have npm > 5.2
If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app to ensure that npx always uses the latest version.
npx create-react-app my-app (to create a new react app)

Upgrading React version and it's dependencies by reading package.json

I have an existing project, which has react#15 and all it's dependencies according to that. But now I have to upgrade to react#16 along with it's dependencies. Now, the problem is - there are a lot of dependencies and it is very time consuming to identify version of each dependency.
So, I was wondering if there was a way where I could upgrade the versions of React and it's dependencies mentioned in package.json, without manually modifying the package.json file.
Using npm
Latest version while still respecting the semver in your package.json: npm update <package-name>.
So, if your package.json says "react": "^15.0.0" and you run npm update react your package.json will now say "react": "^15.6.2" (the currently latest version of react 15).
But since you want to go from react 15 to react 16, that won't do.
Latest version regardless of your semver: npm install --save react#latest.
If you want a specific version, you run npm install --save react#<version> e.g. npm install --save react#16.0.0.
https://docs.npmjs.com/cli/install
Using yarn
Latest version while still respecting the semver in your package.json: yarn upgrade react.
Latest version regardless of your semver: yarn upgrade react#latest.
https://yarnpkg.com/lang/en/docs/cli/upgrade/
if you want to update your react and react-dom version in your existing react step then run this command I hope You get the latest version of react and react-dom.
Thanks
npm install react#latest react-dom#latest
I found a nice article here.
All we need to do (for npm, globally) is:
sudo npm install -g npm-check-updates
Then run it like below:
ncu -u
It will show you all dependencies (upgraded) nicely like below:
Upgrading /home/ajay/Documents/react-beast/package.json
[====================] 7/7 100%
#testing-library/user-event ^11.4.2 → ^13.0.16
react ^17.0.1 → ^17.0.2
react-dom ^17.0.1 → ^17.0.2
react-scripts 4.0.1 → 4.0.3
web-vitals ^1.1.0 → ^1.1.1
Try running ncu -u again immediately after above and you will a message like this:
Upgrading /home/ajay/Documents/react-beast/package.json
[====================] 7/7 100%
All dependencies match the latest package versions :)
Do a npm install after that and you should have all the latest versions for all your dependencies for your project.
To me this was the nicest and cleanest solution (well - in most cases) if we need to keep our (npm/React) project - latest and greatest - rather than wasting time on manually updating the versions.
I highly recommend using yarn upgrade-interactive to update React, or any Node project for that matter. It lists your packages, current version, the latest version, an indication of a Minor, Major, or Patch update compared to what you have, plus a link to the respective project.
You run it with yarn upgrade-interactive --latest, check out release notes if you want, go down the list with your arrow keys, choose which packages you want to upgrade by selecting with the space bar, and hit Enter to complete.
Npm-upgrade is ok but not as slick.
Some packages are interdependent and are to be used with the same versions of each other... for example recently had a problem with npm react#16.5.2 and react-dom#16.5.2. I wanted to use a hook but could not so I ran:
npm update react#latest react-dom#latest
this did not work... went into some of the react.js blogs/documentation and found that these particular packages are interdependent and the versions must be the same. To update these:
npm uninstall react
npm uninstall react-dom
npm install react#^16.8.0 react-dom#^16.8.0
And everything worked just fine. Look for stable releases. Read the documentation.
https://reactjs.org/blog/2019/02/06/react-v16.8.0.html
https://reactjs.org/warnings/invalid-hook-call-warning.html
Yes, you can use Yarn or NPM to edit your package.json.
yarn upgrade [package | package#tag | package#version | #scope/]... [--ignore-engines] [--pattern]
Something like:
yarn upgrade react#^16.0.0
Then I'd see what warns or errors out and then run yarn upgrade [package]. No need to edit the file manually. Can do everything from the CLI.
Or just run yarn upgrade to update all packages to latest, probably a bad idea for a large project. APIs may change, things may break.
Alternatively, with NPM run npm outdated to see what packages will be affected. Then
npm update
https://yarnpkg.com/lang/en/docs/cli/upgrade/
https://docs.npmjs.com/getting-started/updating-local-packages
If you want to update react use npx update react on the terminal.
If you want to update any specific version from the package.json you can update the version of the package by doing ==>
yarn add package-name#version-number
or
npm install --save package-name#version-number
If you want to update all packages to the latest version you can run command ==>
npm audit fix --force
you can update all of the dependencies to their latest version by
npm update
You can just use this command
npm install -g create-react-app
if you don't have root user permissions then use this with
sudo npm install -g create-react-app
👇️ with NPM
npm install react#latest react-dom#latest
👇️ only if you use TypeScript
npm install --save-dev #types/react#latest #types/react-dom#latest
------------------------------
👇️ with YARN
yarn add react#latest react-dom#latest
👇️ only if you use TypeScript
yarn add #types/react#latest #types/react-dom#latest --dev
Use this command to update react npm install --save react#16.12.0
Don't forget to change 16.12.0 to the latest version or the version you need to setup.
No need to install react, just do the following command in terminal in Linux/Mac or cmd in Windows:
npx create-react-app my-app
my-app - is a optional name, you may name it whatever you want it to.
It would create the updated template for your next project up and ready!
In terminal run the commands to update the react versions
$npm i react#next react-dom#next

Resources