-->

Issue with babel-jest dependency when running npm

2020-06-09 07:08发布

问题:

All I am doing is running create-react-app and cd'ing into the app, then trying to run npm/yarn start. I get the following error/output/log. I have walked through all of the suggested steps. The only thing that works is the SKIP_PREFLIGHT_CHECK=true in my .env as the last resort for both Yarn and npm. I have recently updated to Mojave and had to reinstall my Xcode if people have had a similar experience.

If the image isn't sufficient, I pasted the output below.

Thanks so much for your help in advance...Jason

Console Screenshot

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

回答1:

I just had the same problem. For some reason the package ended up in a node_modules directory in my home directory. It also gave me the exact same error for the jest package.

I'm not sure of the correct way to fix this as npm uninstall -g babel-jest and yarn global remove babel-jest do not do anything.

I fixed it by just blowing away the folders that cause the problem: bash rm -rf ~/node_modules/babel-jest ~/node_modules/jest

It would be great to know how those packages ended up there, and the proper way of getting rid of them, but for now just deleting the folders is enough to get CRA dev server running without skipping the preflight check.



回答2:

This problem can occur if there is node_modules in a parent directory of the folder where you run the application. I solved the problem by deleting the node_modules directory.



回答3:

The problem seems to have reappeared in create-react-app 3.0.0.

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

  "babel-jest": "24.7.1"

As abisuq pointed out in https://github.com/facebook/create-react-app/issues/6756#issuecomment-489562571 adding a version resolutions into package.json temporarily fixes the problem.

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

Update: It has been fixed in create-react-app 3.0.1. If upgrading is an option you can run

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

or

yarn add --exact react-scripts@3.0.1


回答4:

I too faced a similar problem and was able to resolve the issue following below steps.

  1. Create a .env file in your project root directory and add the following statement

SKIP_PREFLIGHT_CHECK=true

  1. Save the file

  2. Remove node_modules, yarn.lock, package.lock

  3. Then reinstall node_modules with

npm install

This should work



回答5:

Ok I finally found a solution after trying everything. Here is what finally worked:

  • First of all, read the error message in the cmd from the beginning. They'll tell you what module is causing the problem. You probably have an older version installed on your computer. Example: babel-jest version 2.4.4
  • Go to your Node.js folder c:/Users/(your user)/node_modules and find that module and simply delete it.
  • Go back to the cmd and run npm start. You might get the same error. But it'll be for a different module. Just delete it the same way and repeat until it runs.


回答6:

  1. run: npm ls babel-jest

got : babelk-jest@24.7.1 & babel-jest@24.8.0 installed

this means install tow babel-jest with diff version

  1. run:npm uninstall babel-jest@24.8.0 fix my issue


回答7:

I had this issue and finally solved it easily. As probably you know, when we use create-react-app, Jest is configured by default and you don't need to install Jest (when we use Webpack, we can install Jest). So, if you use Create-react-app and install Jest by mistake:

  1. Firstly uninstall it (remember that if you use: (npm install --save-dev jest), you can remove jest directly from Package.json or use: (npm uninstall --save-dev jest)
  2. Delete package-lock.json (not package.json)
  3. Delete the node_modules
  4. npm install

Now you don't receive an error and you can easily use: (npm start) or (npm test)

Also, It is worth mentioning that I installed the following tools to write my test in React component: (npm install --save-dev enzyme enzyme-adapter-react-16 jest-enzyme) and wrote my test by using jest and enzyme. Then I easily use: (npm test)

Good Luck!



回答8:

I had the exact same issue. What I tried was to create the react app outside the directory that had the node_modules folder. After that, started the app with yarn start and didn't have the error anymore.



回答9:

I had the same problem, and I fixed this issue. For some reason because node_modules on my local. I removed babel-jest & jest. After that, npm start. I'm not sure this solved incorrect but this correct for me.



回答10:

I solved this issue by deleting the node_modules folder and package-lock.json file I accidentally had installed in the root of my user.

For me in Mac the path was:

Macintosh HD -> Users -> "My-user-name"

I discover that might be the problem by running npm ls babel-jest on terminal. That showed me that there was another babel-jest up in the tree.

After deleting these two I did npm install in my app where I previously had deleted the node_modules folder and package-lock.json.

Now it's running ok !