javascript - 如何使用 Jest 修复 React App 的依赖错误

标签 javascript node.js reactjs jestjs yarnpkg

所以我在一个新项目中安装了 Jest,由于以下错误,该应用程序停止运行。

总结:它告诉我我已经手动安装了对 node_modules 的依赖,这是我没有做的,它要求我删除我的整个 node_modulesyarn.lock。但这些步骤不起作用。

注意:从 package.json 中删除依赖项,然后删除 node_modulesyarn.lock 确实修复了问题,但是当我再次安装jest时,它又陷入了同样的问题。

我安装 jest 的步骤,here :

$ yarn add --dev react-test-renderer
$ yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react react-test-renderer

错误信息

$ 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:

  "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:

  C:\WORK\plotting-a-chart\node_modules\jest (version: 26.6.3)

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

If you would 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 "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 C:\WORK\plotting-a-chart\node_modules\jest is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls jest in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed 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!

我尝试了所有步骤,我已经使用了 yarn,我什至在执行步骤 1 到 4 之前清除了我的 yarn 缓存。

  1. 删除项目文件夹中的 package-lock.json(不是 package.json!)和/或 yarn.lock
  2. 删除项目文件夹中的node_modules
  3. 从项目文件夹的 package.json 文件中的依赖项和/或 devDependencies 中删除“jest”;
  4. 运行 npm install 或 yarn,具体取决于您使用的包管理器。

我没有想法。

// package.json
{
  "name": "project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "node-sass": "^5.0.0",
    "prismjs": "^1.23.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-hook-form": "^6.15.5",
    "react-icons": "^4.2.0",
    "react-resizable": "^1.11.1",
    "react-scripts": "4.0.3",
    "react-vis": "^1.11.7"
  },
  "scripts": {
    "dev": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "unit": "jest"
  },
  "eslintConfig": {
    "extends": [
      "react-app"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/preset-env": "^7.13.12",
    "@babel/preset-react": "^7.13.13",
    "babel-jest": "^26.6.3",
    "babel-plugin-root-import": "^6.6.0",
    "jest": "^26.6.3",
    "react-test-renderer": "^17.0.2"
  }
}
// babel.config.js
module.exports = {
   presets: ['@babel/preset-env', '@babel/preset-react'],
 };

最佳答案

您不必 npm install jest。错误告诉你:

Don't try to install it manually.

这是它在 react documentation 上的内容

If you use Create React App, Jest is already included out of the box with useful defaults.

要解决您的问题,您应该运行此命令:

npm uninstall --save-dev jest
npm uninstall --save jest
rm node_modules 
npm i

关于javascript - 如何使用 Jest 修复 React App 的依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66891027/

相关文章:

node.js - NodeJS Commander 结构化子命令

node.js - 如何使用 Node 在 koa.js 中发送响应

javascript - 在 react jsx 中显示 html 标记

javascript - 在React js中实现带有文本输入的单选列表

javascript - 使用 Mongoose 更新包含唯一值的对象数组

php - 如何使用连续的服务器端处理来制作不断更新数据库信息的新闻源?

node.js - 如何使用 javascript 和 pdfkit 将文本添加到 pdf?

javascript - React-子函数调用父函数

javascript - 等距引擎绘图问题

javascript - 将带有事件委托(delegate)的事件监听器附加到 Jupyter 笔记本?