[英]Required babel-jest dependency
I am new in the topic of React. 我是React的新话题。 I started it recently and I have a problem when I want to start a React app.
我最近启动了它,当我想启动一个React应用程序时遇到了问题。 '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. 我试图在Internet上找到解决方案,但我并不幸运。 Where is the problem, please?
请问问题出在哪里?
So it looks like what might have happened was that after you created a new app using create-react-app
. 因此,它看起来像什么可能发生的情况是,你创建一个使用新的应用程序后,
create-react-app
。 you then manually added the babel-jest
package with yarn add babel-jest
. 然后,您可以手动添加
babel-jest
包和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
. 你并不需要添加
babel-jest
手工包,因为它已经捆绑到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
. 首先,打开
package.json
并删除包含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: 然后,由于您正在使用
yarn
,因此请在项目根文件夹中执行以下操作:
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). 这将删除手动添加的
babel-jest
软件包,然后为您重新安装最初与create-react-app
一起提供的所有软件包(其中包括已经捆绑在一起的babel-jest
软件包)。
Based on your comment, I believe that has happened is this: 根据您的评论,我相信发生的事情是这样的:
You used npm init
which initialized a new nodejs project, with its own package.json
file. 您使用
npm init
初始化了一个新的nodejs项目,并使用其自己的package.json
文件。
Then in that folder, you used create-react-app
to create a test
sub-folder - this initialized another project with its own package.json
file. 然后,在该文件夹中,您使用
create-react-app
创建了一个test
子文件夹-这使用其自己的package.json
文件初始化了另一个项目。
For some reason, the project in the parent folder (which you initialized with npm init
) has a reference to babel-jest
- and this is conflicting with the app in the child folder which you created with create-react-app
. 出于某种原因,父文件夹(您使用
npm init
)中的项目引用了babel-jest
这与您使用create-react-app
的子文件夹中的应用程序冲突。
Keep in mind that npm
and yarn
are both package managers for nodejs projects. 请记住,
npm
和yarn
都是nodejs项目的包管理器。 Each of them have an init
command which can be used to initialize a new project, setting up a package.json
file. 它们每个都有一个
init
命令,可用于初始化新项目,并设置package.json
文件。 For the most part, you will never need to use both npm
and yarn
within a single project. 在大多数情况下,您将永远不需要在单个项目中同时使用
npm
和yarn
。 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. create-react-app
是另一个命令,可用于初始化具有许多内置初始设置的新ReactJS项目。该命令内置babel-jest
,这可能导致与父文件夹的package.json
产生某些冲突package.json
文件或可能是全局安装的babel-jest
软件包。
As a general rule, you should only use 1 of the following: * npm init
initializes a project at the current folder. 通常,您仅应使用以下选项之一:*
npm init
在当前文件夹中初始化一个项目。 * yarn init
initializes a project at the current folder. *
yarn init
初始化当前文件夹中的项目。 * create-react-app .
*
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. 对于您当前的情况,由于您正在处理新项目,因此建议您尝试重新初始化一个新的
create-react-app
项目。 Go to the parent folder, outside of the one where you used the npm init
command. 转到父文件夹,该文件夹位于使用
npm init
命令的文件夹之外。 Create a project folder, and then in that folder, run create-react-app .
创建一个项目文件夹,然后在该文件夹中运行
create-react-app .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.