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

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

我所做的就是运行创建-react-app 并进入应用程序,然后尝试运行 npm/yarn start。我得到以下 error/output/log。我已经完成了所有建议的步骤。唯一可行的是我的 .env 中的 SKIP_PREFLIGHT_CHECK=true 作为两者的最后手段 纱线和 npm。我最近更新到 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

我刚刚遇到了同样的问题。由于某种原因,该包最终位于我的主目录中的 node_modules 目录中。对于 jest 包,它也给了我完全相同的错误。

我不确定 正确 解决此问题的方法,因为 npm uninstall -g babel-jestyarn global remove babel-jest 什么都不做。

我通过清除导致问题的文件夹来修复它: bash rm -rf ~/node_modules/babel-jest ~/node_modules/jest

很高兴知道这些包是如何在那里结束的,以及摆脱它们的正确方法,但现在只需删除文件夹就足以获取 CRA 开发服务器 运行 而无需跳过飞行前检查。

如果 运行 应用程序所在文件夹的父目录中存在 node_modules,则可能会出现此问题。我通过删除 node_modules 目录解决了这个问题。

我遇到了完全相同的问题。 我尝试的是在具有 node_modules 文件夹的目录之外创建反应应用程序。 之后,用 yarn start 启动应用,就没有报错了。

  1. 运行: npm ls babel-jest

已安装:babelk-jest@24.7.1 & babel-jest@24.8.0

这意味着安装带有 diff 版本的两个 babel-jest

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

这个问题似乎在create-react-app 3.0.0 中又出现了。

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

  "babel-jest": "24.7.1"

正如 abisuq 在 https://github.com/facebook/create-react-app/issues/6756#issuecomment-489562571 中指出的那样 将版本分辨率添加到 package.json 中可以暂时解决问题

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

更新: 已在 create-react-app 3.0.1 中修复。 如果可以选择升级,您可以 运行

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

yarn add --exact react-scripts@3.0.1

我遇到了这个问题,最后轻松解决了。 你可能知道,当我们使用 create-react-app 时,Jest 是默认配置的,你不需要安装 Jest(当我们使用 Webpack 时,我们可以安装 Jest)。所以,如果你使用 Create-react-app 并错误地安装了 Jest:

  1. 首先卸载它(记住如果你使用:(npm install --save-dev jest),你可以直接从Package.json中删除jest或使用:(npm uninstall --save-dev jest
  2. 删除package-lock.json(不是package.json)
  3. 删除node_modules
  4. npm install

现在您不会收到错误,您可以轻松使用:(npm start) 或 (npm test)

此外,值得一提的是,我安装了以下工具来编写我在 React 组件中的测试: (npm install --save-dev enzyme enzyme-adapter-react-16 jest-enzyme) 并使用 jest 和 enzyme 编写了我的测试。然后我轻松使用:(npm test)

祝你好运!

我也遇到了类似的问题,并且能够按照以下步骤解决问题。

  1. 在你的项目根目录下创建一个.env文件,添加如下语句

SKIP_PREFLIGHT_CHECK=true

  1. 保存文件

  2. 删除node_modules、yarn.lock、package.lock

  3. 然后用

  4. 重新安装node_modules

npm install

这应该有效

好的,我尝试了所有方法后终于找到了解决方案。这是最终起作用的方法:

  • 首先从头看cmd中的报错信息。他们会告诉您是哪个模块导致了问题。您的计算机上可能安装了旧版本。示例:babel-jest 版本 2.4.4
  • 转到您的 Node.js 文件夹 c:/Users/(您的用户)/node_modules 并找到该模块并将其删除。
  • 返回 cmd 并 运行 npm start。你可能会得到同样的错误。但它将用于不同的模块。只需以相同的方式删除它并重复直到它 运行s。

我遇到了同样的问题,我解决了这个问题。出于某种原因,因为 node_modules 在我的本地。我删除了 babel-jest & jest。之后,npm 启动。我不确定这是否解决了错误,但这对我来说是正确的。

我通过删除不小心安装在用户根目录中的 node_modules 文件夹和 package-lock.json 文件解决了这个问题。

对于 Mac 中的我来说,路径是:

Macintosh HD -> 用户 -> "My-user-name"

我发现这可能是终端上 运行 npm ls babel-jest 的问题。这告诉我树上还有另一个 babel-jest。

删除这两个后,我在我的应用程序中安装了 npm,我之前删除了 node_modules 文件夹和 package-lock.json.

现在 运行 可以了!

我通过从高于项目根目录的文件夹中删除 node_modules 文件夹和 package-lock.json 文件解决了这个问题。我不小心将 node_modules 安装在了更高的文件夹中。

我有:

  • desktop/code/node_modules(删除此修复)
  • desktop/code/package-lock.json(删除此修复)
  • desktop/code/project/node_modules
  • desktop/code/project/package-lock.json

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

试试这个命令,看看哪些包在版本中产生冲突。

npm ls babel-jest

用更新的软件包之一替换冲突的软件包。

我尝试了上面所有的书面解决方案。但其中 none 有效。 我通过删除“C:\node_modules”文件夹解决了问题。 然后删除项目node_modules和包-lock.json。最后,npm install 并重新启动。它奏效了。

我也遇到了这个问题。当我尝试使用 npm start 运行 客户端时,会出现错误,告诉我它需要 babel-jest: ^24.9.0.

我注意到在我的客户端中,babel-jest 的版本是 ^24.9.0,但在我的服务器中,我有 "jest": "^26.6.3"。

我所要做的就是在服务器端源代码中将 "jest": "^26.6.3" 更改为 "jest": "^24.9.0" ,删除我的 package-lock.json 作为以及 node_modules 在服务器中,再次安装 npm,一切顺利!

我的 react-scripts 版本是 4.0.3。安装私有远程包后突然出现问题,在我的项目根目录下安装了多个babel包node_modules。我通过在项目根级别明确安装有问题的包来解决这个问题,以便它们与预检检查中报告的版本相匹配。

在我的情况下,以下添加修复了它。您需要的包和版本可能不同,您必须检查预检报告。

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

对我来说,这仍然是一种解决方法,但我更喜欢这种方式,而不是按照 中的建议手动删除 node_modules 中的内容。

这是不删除的做法node_modules:

  • 我收到如下错误:

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. However, a different version of jest was detected higher up in the tree:

  • 首先检查版本使用:

    npm ls babel-jest

    在我的例子中输出是这样的:

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

  • 然后通过
    卸载babel-jest npm uninstall babel-jest
    (当看到babel-jest是单一版本的时候,否则按如下版本走)
    npm uninstall babel-jest@27.0.2

  • 然后使用
    安装所需的依赖项 npm i babel-jest@version jest@version
    (第 1 点的版本是哪个版本)

这对我来说就像一个魅力。希望这也能解决你。

在 netlify ci 中遇到此错误,这是对我有用的修复方法: 此方法适用于任何库我得到了 eslint 而不是 babel-jest 的错误。

  1. 通过 npm i -s @babel-jest/VERSION --force

    强制显示错误

    将 VERSION 替换为错误中显示的任何版本(在本例中为 23.6.0), 正确的错误消息将在本地显示

  2. 使用 https://www.npmjs.com/package/npm-check-updates 将您的 package.json 升级到正确的版本

    run ncu -u in dir of package.json after installing the npm package globally

  3. 终于做一个 npm ci

    这将删除 package-lock 和 node_modules 并根据第二步安装新版本

这对我有用。 清除 npm 或 yarn 中的缓存 删除 node_modules 并锁定文件

  • 创建 .env 文件
  • 将“SKIP_PREFLIGHT_CHECK=true”添加到项目中的 .env 文件中。

我遇到了类似的问题,浪费了我 2-3 天的时间 解决这个问题最简单的方法是:

1.go 在 src 之外,并创建 .env 文件。 2.Inside .env 文件,只写这一行并保存: SKIP_PREFLIGHT_CHECK=真 3.then npm 开始

希望这对您有所帮助,编码愉快!!!

我的问题是我同时拥有前端和后端 运行。我将 jest 安装到我的根项目(用于后端),我猜与 pre-installed react jest 有冲突。 我刚刚从后端卸载了 jest,瞧,我现在很开心。我没有任何与 babel 相关的东西。