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

问题描述 投票:9回答:6

我正在做的就是运行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

> [email protected] 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! [email protected] start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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
reactjs macos npm yarnpkg react-scripts
6个回答
12
投票

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

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

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

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


12
投票

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


10
投票

这个问题似乎再次出现在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 [email protected]

要么

yarn add --exact [email protected]

2
投票
  1. 跑:npm ls babel-is

得到:安装了[email protected][email protected]

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

  1. 运行:npm uninstall [email protected]解决我的问题

2
投票

我遇到了这个问题,最后很容易解决了。您可能知道,当我们使用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

祝好运!


0
投票

我有完全相同的问题。我尝试的是在具有node_modules文件夹的目录之外创建react应用程序。之后,启动了纱线启动的应用程序,并且没有错误了。


0
投票

我也面临类似的问题,并能够按照以下步骤解决问题。

  1. 在项目根目录中创建.env文件并添加以下语句

SKIP_PREFLIGHT_CHECK =真

  1. 保存文件
  2. 删除node_modules,yarn.lock,package.lock
  3. 然后重新安装node_modules

npm安装

这应该工作

© www.soinside.com 2019 - 2024. All rights reserved.