简体   繁体   English

在 React 应用程序中运行 npm start 时出现 babel-jest 依赖问题

[英]Issue with babel-jest dependency when running npm start in a React app

All I am doing is running create-react-app and cd'ing into the app, then trying to run npm/yarn start .我所做的只是运行 create-react-app 并 cd'ing 进入应用程序,然后尝试运行npm/yarn start I get the following error/output/log.我收到以下错误/输出/日志。 I have walked through all of the suggested steps.我已经完成了所有建议的步骤。 The only thing that works is the SKIP_PREFLIGHT_CHECK=true in my .env as the last resort for both Yarn and npm.唯一有效的是我的 .env 中的 SKIP_PREFLIGHT_CHECK=true 作为 Yarn 和 npm 的最后手段。 I have recently updated to Mojave and had to reinstall my Xcode if people have had a similar experience.我最近更新到 Mojave,如果人们有类似的经历,我必须重新安装我的 Xcode。

Last login: Tue Oct 30 16:30:24 on ttys002
TheLAB11:~ jasonspiller$ cd repos/react-express-graphql-app/
TheLAB11:react-express-graphql-app jasonspiller$ npm start

> react-express-graphql-app@0.1.0 start /Users/jasonspiller/repos/react-express-graphql-app
> react-scripts start


There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "babel-jest": "23.6.0"

Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-jest was detected higher up in the tree:

  /Users/jasonspiller/node_modules/babel-jest (version: 23.4.2) 

Manually installing incompatible versions is known to cause hard-to-debug issues.

If prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "babel-jest" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:

  5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
     This may help because npm has known issues with package hoisting which may get resolved in future versions.

  6. Check if /Users/jasonspiller/node_modules/babel-jest is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls babel-jest in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed babel-jest.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! react-express-graphql-app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the react-express-graphql-app@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!     /Users/jasonspiller/.npm/_logs/2018-10-30T23_09_42_358Z-debug.log

I just had the same problem.我只是有同样的问题。 For some reason the package ended up in a node_modules directory in my home directory.由于某种原因,该软件包最终位于我的主目录中的node_modules目录中。 It also gave me the exact same error for the jest package.对于jest包,它也给了我完全相同的错误。

I'm not sure of the correct way to fix this as npm uninstall -g babel-jest and yarn global remove babel-jest do not do anything.我不确定解决此问题的正确方法,因为npm uninstall -g babel-jestyarn global remove babel-jest不做任何事情。

I fixed it by just blowing away the folders that cause the problem: bash rm -rf ~/node_modules/babel-jest ~/node_modules/jest我通过删除导致问题的文件夹来修复它: bash rm -rf ~/node_modules/babel-jest ~/node_modules/jest

It would be great to know how those packages ended up there, and the proper way of getting rid of them, but for now just deleting the folders is enough to get CRA dev server running without skipping the preflight check.很高兴知道这些包是如何结束的,以及摆脱它们的正确方法,但现在只需删除文件夹就足以让 CRA 开发服务器运行而无需跳过预检检查。

This problem can occur if there is node_modules in a parent directory of the folder where you run the application.如果运行应用程序的文件夹的父目录中有 node_modules,则可能会出现此问题。 I solved the problem by deleting the node_modules directory.我通过删除 node_modules 目录解决了这个问题。

I too faced a similar problem and was able to resolve the issue following below steps.我也遇到了类似的问题,并且能够按照以下步骤解决问题。

  1. Create a .env file in your project root directory and add the following statement在你的项目根目录中创建一个 .env 文件并添加以下语句

SKIP_PREFLIGHT_CHECK=true SKIP_PREFLIGHT_CHECK=真

  1. Save the file保存文件

  2. Remove node_modules, yarn.lock, package.lock移除 node_modules、yarn.lock、package.lock

  3. Then reinstall node_modules with然后重新安装 node_modules

npm install npm 安装

This should work这应该工作

The problem seems to have reappeared in create-react-app 3.0.0.该问题似乎在 create-react-app 3.0.0 中再次出现。

The react-scripts package provided by Create React App requires a dependency:

  "babel-jest": "24.7.1"

As abisuq pointed out in https://github.com/facebook/create-react-app/issues/6756#issuecomment-489562571 adding a version resolutions into package.json temporarily fixes the problem .正如 abisuq 在https://github.com/facebook/create-react-app/issues/6756#issuecomment-489562571中指出的那样,在package.json 中添加版本分辨率暂时解决了问题

"resolutions": {
  "babel-jest": "24.7.1"
},

Update: It has been fixed in create-react-app 3.0.1 .更新:它已在create-react-app 3.0.1中修复。 If upgrading is an option you can run如果升级是一个选项,您可以运行

npm install --save --save-exact react-scripts@3.0.1

or或者

yarn add --exact react-scripts@3.0.1
  1. run: npm ls babel-jest运行: npm ls babel-jest

got : babelk-jest@24.7.1 & babel-jest@24.8.0 installed得到:安装了 babelk-jest@24.7.1 & babel-jest@24.8.0

this means install tow babel-jest with diff version这意味着使用 diff 版本安装 tow babel-jest

  1. run: npm uninstall babel-jest@24.8.0 fix my issue运行: npm uninstall babel-jest@24.8.0修复我的问题

I had this issue and finally solved it easily.我遇到了这个问题,终于轻松解决了。 As probably you know, when we use create-react-app, Jest is configured by default and you don't need to install Jest (when we use Webpack, we can install Jest).可能你知道,当我们使用 create-react-app 时,Jest 是默认配置的,你不需要安装 Jest(当我们使用 Webpack 时,我们可以安装 Jest)。 So, if you use Create-react-app and install Jest by mistake:因此,如果您使用 Create-react-app 并错误地安装了 Jest:

  1. Firstly uninstall it (remember that if you use: ( npm install --save-dev jest ), you can remove jest directly from Package.json or use: ( npm uninstall --save-dev jest )首先卸载它(请记住,如果你使用:( npm install --save-dev jest ),你可以直接从 Package.json 中删除 jest 或使用:( npm uninstall --save-dev jest
  2. Delete package-lock.json (not package.json)删除 package-lock.json(不是 package.json)
  3. Delete the node_modules删除 node_modules
  4. npm install

Now you don't receive an error and you can easily use: (npm start) or (npm test)现在您不会收到错误消息,您可以轻松使用:(npm start) 或 (npm test)

Also, It is worth mentioning that I installed the following tools to write my test in React component: ( npm install --save-dev enzyme enzyme-adapter-react-16 jest-enzyme ) and wrote my test by using jest and enzyme.另外,值得一提的是,我安装了以下工具来在 React 组件中编写我的测试:( npm install --save-dev enzyme enzyme-adapter-react-16 jest-enzyme )并使用 jest 和酶编写我的测试。 Then I easily use: ( npm test )然后我很容易使用:( npm test

Good Luck!祝你好运!

Ok I finally found a solution after trying everything.好的,我在尝试了一切之后终于找到了解决方案。 Here is what finally worked:这是最终奏效的方法:

  • First of all, read the error message in the cmd from the beginning.首先,从头开始阅读cmd中的错误信息。 They'll tell you what module is causing the problem.他们会告诉你是什么模块导致了问题。 You probably have an older version installed on your computer.您的计算机上可能安装了旧版本。 Example: babel-jest version 2.4.4示例:babel-jest 版本 2.4.4
  • Go to your Node.js folder c:/Users/(your user)/node_modules and find that module and simply delete it.转到您的 Node.js 文件夹 c:/Users/(your user)/node_modules 并找到该模块并简单地删除它。
  • Go back to the cmd and run npm start.返回 cmd 并运行 npm start。 You might get the same error.你可能会得到同样的错误。 But it'll be for a different module.但这将用于不同的模块。 Just delete it the same way and repeat until it runs.只需以相同的方式删除它并重复直到它运行。

I fixed the issue by removing the node_modules folder and package-lock.json file from a folder that was higher than the root of the project.我通过从高于项目根目录的文件夹中删除 node_modules 文件夹和 package-lock.json 文件来解决此问题。 I had installed node_modules accidentally in a higher folder.我不小心将 node_modules 安装在更高的文件夹中。

I had:我有:

  • desktop/code/node_modules (remove this to fix) desktop/code/node_modules(删除此修复)
  • desktop/code/package-lock.json (remove this to fix) desktop/code/package-lock.json (删除此修复)
  • desktop/code/project/node_modules桌面/代码/项目/node_modules
  • desktop/code/project/package-lock.json桌面/代码/项目/package-lock.json

I was having this issue as well.我也有这个问题。 When I tried to run the client using npm start, the error would occur, telling me that it required babel-jest: ^24.9.0.当我尝试使用 npm start 运行客户端时,会出现错误,告诉我它需要 babel-jest: ^24.9.0.

I noticed that in my client, babel-jest was version ^24.9.0, but in my server, I had "jest": "^26.6.3".我注意到在我的客户端中,babel-jest 是 ^24.9.0 版本,但在我的服务器中,我有“jest”:“^26.6.3”。

All I had to do was change "jest": "^26.6.3" to "jest": "^24.9.0" in the server side source code, delete my package-lock.json as well as node_modules in server, npm install again, and good to go!我所要做的就是在服务器端源代码中将 "jest": "^26.6.3" 更改为 "jest": "^24.9.0",删除我的 package-lock.json 以及服务器 npm 中的 node_modules再次安装,一切顺利!

I had the exact same issue.我有完全相同的问题。 What I tried was to create the react app outside the directory that had the node_modules folder.我尝试的是在包含 node_modules 文件夹的目录之外创建反应应用程序。 After that, started the app with yarn start and didn't have the error anymore.之后,使用 yarn start 启动应用程序,不再出现错误。

I had the same problem, and I fixed this issue.我有同样的问题,我解决了这个问题。 For some reason because node_modules on my local.出于某种原因,因为我本地的 node_modules 。 I removed babel-jest & jest.我删除了 babel-jest & jest。 After that, npm start.之后,npm start。 I'm not sure this solved incorrect but this correct for me.我不确定这解决的不正确,但这对我来说是正确的。

I solved this issue by deleting the node_modules folder and package-lock.json file I accidentally had installed in the root of my user.我通过删除我不小心安装在用户根目录中的 node_modules 文件夹和 package-lock.json 文件解决了这个问题。

For me in Mac the path was:对于 Mac 中的我来说,路径是:

Macintosh HD -> Users -> "My-user-name" Macintosh HD -> 用户 -> “我的用户名”

I discover that might be the problem by running npm ls babel-jest on terminal.我通过在终端上运行 npm ls babel-jest 发现这可能是问题所在。 That showed me that there was another babel-jest up in the tree.这向我表明,树上还有另一个巴别塔。

After deleting these two I did npm install in my app where I previously had deleted the node_modules folder and package-lock.json.删除这两个后,我在我的应用程序中执行了 npm install,我之前删除了 node_modules 文件夹和 package-lock.json。

Now it's running ok !现在运行正常!

我通过删除项目目录中的 node_module 文件夹来解决这个问题

try this command and see which packages create conflicts in versions.试试这个命令,看看哪些包会在版本中产生冲突。

npm ls babel-jest

replace the conflicted packages with updated one of that package.用更新的软件包之一替换冲突的软件包。

I tried all of written solutions above.我尝试了上面所有的书面解决方案。 But none of them worked.但他们都没有工作。 I solved problem by deleting "C:\node_modules" folder.我通过删除“C:\node_modules”文件夹解决了问题。 Then delete project node_modules and package-lock.json.然后删除项目 node_modules 和 package-lock.json。 Finally, npm install and start again.最后, npm install 并重新开始。 And it worked.它奏效了。

My react-scripts version is 4.0.3 .我的 react-scripts 版本是4.0.3 The problem suddenly happened after installing a private remote package, which installed multiple babel packages in my project root's node_modules .安装私有远程包后突然出现问题,该包在我的项目根目录下安装了多个 babel 包node_modules I solved this by installing the problematic packages on the project root's level explicitly, so that they match the versions reported in the preflight check.我通过在项目根级别显式安装有问题的包来解决这个问题,以便它们与预检检查中报告的版本相匹配。

Following addings fixed it in my case.在我的情况下,添加后修复了它。 Your needed packages and versions may differ, you have to check the preflight report.您需要的软件包和版本可能不同,您必须检查预检报告。

yarn add babel-jest@^26.6.0
yarn add babel-loader@8.1.0

For me its still a workaround, but i prefer this way instead of removing something in node_modules manually as suggested in https://stackoverflow.com/a/53093421/4840661 .对我来说它仍然是一种解决方法,但我更喜欢这种方式,而不是按照https://stackoverflow.com/a/53093421/4840661中的建议手动删除node_modules中的某些内容。

This is the approach without deleting node_modules:这是不删除node_modules的方法:

  • I recieved error like:我收到如下错误:

The react-scripts package provided by Create React App requires a dependency: "jest": "26.6.0" Don't try to install it manually: your package manager does it automatically. Create React App 提供的 react-scripts 包需要依赖: "jest": "26.6.0" 不要尝试手动安装:你的包管理器会自动安装。 However, a different version of jest was detected higher up in the tree:但是,在树的较高位置检测到不同版本的 jest:

  • First check the versions using:首先使用以下命令检查版本:

    npm ls babel-jest

    In my case output was like this:在我的情况下,输出是这样的:

jest@27.0.3开玩笑@27.0.3
└─┬ @jest/core@27.0.3 └─┬ @jest/core@27.0.3
└─┬ jest-config@27.0.3 └─┬ jest-config@27.0.3
└── babel-jest@27.0.2 └── babel-jest@27.0.2

  • After that uninstall babel-jest by之后通过卸载 babel-jest
    npm uninstall babel-jest
    (When you see the single version of babel-jest,otherwise go as versions as follows) (当你看到babel-jest的单一版本时,其他版本如下)
    npm uninstall babel-jest@27.0.2

  • Then install required dependencies using然后使用安装所需的依赖项
    npm i babel-jest@version jest@version
    (Where version is the one come in 1st point) (其中版本是第 1 点)

This works for me like a charm.这对我来说就像一个魅力。 Hope this solves you too.希望这也能解决你。

Got this error in the netlify ci, here's the fix that worked for me: this method works for any lib i got a error for eslint instead of babel-jest.在 netlify ci 中出现此错误,这是对我有用的修复:此方法适用于任何 lib 我遇到了 eslint 而不是 babel-jest 的错误。

  1. force the error to be shown by npm i -s @babel-jest/VERSION --force强制 npm i -s @babel-jest/VERSION --force 显示错误

    replace VERSION with whatever version shows in the error(23.6.0 in this case), the correct error message will be shown locally将 VERSION 替换为错误中显示的任何版本(在本例中为 23.6.0),正确的错误消息将在本地显示

  2. use https://www.npmjs.com/package/npm-check-updates to upgrade your package.json flie with the correct versions使用https://www.npmjs.com/package/npm-check-updates用正确的版本升级你的 package.json 文件

    run ncu -u in dir of package.json after installing the npm package globally全局安装 npm 包后,在 package.json 的目录中运行 ncu -u

  3. finally do a npm ci最后做一个 npm ci

    this will delete the package-lock and node_modules and install the new versions based on the second step这将删除 package-lock 和 node_modules 并根据第二步安装新版本

this work for me.这对我有用。 clear caches in npm or yarn remove node_modules and lock files清除 npm 或 yarn 中的缓存删除 node_modules 并锁定文件

  • create .env file创建 .env 文件
  • add " SKIP_PREFLIGHT_CHECK=true " to an .env file in your project.将“SKIP_PREFLIGHT_CHECK=true”添加到项目中的 .env 文件中。

my issue was that I have both frontend and backend that I am running concurrently.我的问题是我有同时运行的前端和后端。 I installed jest to my root project (for backend) and I guess there is collision with pre-installed react jest .我将 jest 安装到我的根项目(用于后端),我猜想与pre-installed react jest有冲突。 I just uninstalled jest from backend and voila I am happy now.我刚刚从后端卸载了 jest,瞧,我现在很高兴。 I don't have anything related with babel.我没有任何与 babel 相关的内容。

  • At first I installed these (my react version was "^17.0.2")起初我安装了这些(我的反应版本是“^17.0.2”)
    1. npm install --save-dev jest npm install --save-dev jest

    2. npm install --save-dev enzyme enzyme-to-json npm install --save-dev 酶酶转json

    3. npm install @wojtekmaj/enzyme-adapter-react-17 npm install @wojtekmaj/enzyme-adapter-react-17

  • After that, I had the same problem.之后,我遇到了同样的问题。
  • Then I did these steps :)然后我做了这些步骤:)
    1. Added '.env' file in my project root directory and add: SKIP_PREFLIGHT_CHECK=true在我的项目根目录中添加了“.env”文件并添加:SKIP_PREFLIGHT_CHECK=true
    2. Removed node_modules & package.lock删除了 node_modules 和 package.lock
    3. npm install npm 安装

    4. npm start npm 开始

i had the similar problem and wasted my 2-3 days The easiest way to solve this problem is :我有类似的问题,浪费了我的 2-3 天解决这个问题的最简单方法是:

1.go outside of src, and create .env file. 1. 到 src 之外,创建 .env 文件。 2.Inside .env file, just write this single line and save it: SKIP_PREFLIGHT_CHECK=true 3.then npm start 2.在 .env 文件中,只写一行并保存: SKIP_PREFLIGHT_CHECK=true 3.然后 npm start

Hope this helps, Happy coding!!!希望这有帮助,快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM