Search code examples
javascriptnode.jsreactjsjestjsyarnpkg

How to fix dependency error with React App using Jest


So I installed Jest in a new project and the app stopped running, due to the error below.

Summary: It's telling me that I've manually installed a dependency to node_modules, something I didn't do, and it's asking me to delete my entire node_modules and yarn.lock. But those steps aren't working.

Note: Removing the dependency from package.json then deleting node_modules and yarn.lock does fix the problem, but when I install jest again, it falls into the same problem.

The steps I took to install jest, here:

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

The error message

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

I tried doing all the steps, I already use yarn, I even cleared my yarn cache before doing the steps 1 to 4.

  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.

I'm out of ideas.

// 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'],
 };

Solution

  • You don't have to npm install jest. The error is telling you :

    Don't try to install it manually.

    Here is what it says on react documentation

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

    To solve your issue you should run this :

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