问题
I am new in the topic of React. I started it recently and I have a problem when I want to start a React app. 'yarn start' shows this error:
The react-scripts package provided by Create React App requires a dependency: "babel-jest": "23.6.0"
How can I overcome this error, please.
I tried to find a solution on Internet but I was not lucky. Where is the problem, please?
回答1:
So it looks like what might have happened was that after you created a new app using create-react-app
. you then manually added the babel-jest
package with yarn add babel-jest
. At least, that's how I was able to replicate on my machine the error that you quoted.
You do not need to add the babel-jest
package manually, as it is already bundled into create-react-app
. So, you need to follow the instructions that were provided alongside the above error in order to remove the package:
First, open package.json
and remove the dependency line that includes babel-jest
. It might look something like this:
// package.json
{
"name": "babel",
"version": "0.1.0",
"private": true,
"dependencies": {
"babel-jest": "23.6.0", <----------- REMOVE THIS LINE
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-scripts": "2.1.8"
},
Then, since you're using yarn
, do the following from within your project root folder:
rm -rf node_modules
rm yarn.lock
yarn install
This removes the manually added babel-jest
package and then gives you a fresh install of all of the packages that originally came with create-react-app
(which includes the babel-jest
package that is already bundled in).
Update
Based on your comment, I believe that has happened is this:
You used
npm init
which initialized a new nodejs project, with its ownpackage.json
file.Then in that folder, you used
create-react-app
to create atest
sub-folder - this initialized another project with its ownpackage.json
file.For some reason, the project in the parent folder (which you initialized with
npm init
) has a reference tobabel-jest
- and this is conflicting with the app in the child folder which you created withcreate-react-app
.
Keep in mind that npm
and yarn
are both package managers for nodejs projects. Each of them have an init
command which can be used to initialize a new project, setting up a package.json
file. For the most part, you will never need to use both npm
and yarn
within a single project. Just choose which one you want to use and stick with that one.
create-react-app
is another command which can be used to initialize a new ReactJS project with a lot of initial setup built in. This has babel-jest
built into it, which is what might be leading to some conflicts with the parent folder's package.json
file or possibly a globally installed babel-jest
package.
As a general rule, you should only use 1 of the following:
* npm init
initializes a project at the current folder.
* yarn init
initializes a project at the current folder.
* create-react-app .
initializes a project at the current folder.
For your current situation, since you are working with a new project, I would recommend that you try to start over in initializing a new create-react-app
project. Go to the parent folder, outside of the one where you used the npm init
command. Create a project folder, and then in that folder, run create-react-app .
来源:https://stackoverflow.com/questions/55204598/required-babel-jest-dependency