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

IT技术 reactjs macos npm yarnpkg react-scripts
2021-05-07 01:11:42

我所做的就是运行 create-react-app 和 cd'ing 进入应用程序,然后尝试运行npm/yarn start. 我收到以下错误/输出/日志。我已经完成了所有建议的步骤。唯一有效的是 .env 中的 SKIP_PREFLIGHT_CHECK=true 作为 Yarn 和 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
4个回答

我只是遇到了同样的问题。出于某种原因,该包最终出现node_modules在我的主目录中的一个目录中。它还给了我完全相同的jest错误

我是不知道正确的解决这一问题的办法npm uninstall -g babel-jest,并yarn global remove babel-jest没有做任何事情。

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

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

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

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

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

SKIP_PREFLIGHT_CHECK=真

  1. 保存文件

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

  3. 然后重新安装 node_modules

安装

这应该工作

该问题似乎在 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