简体   繁体   中英

Error: Failed to load parser 'babel-eslint' declared in '.eslintrc': Cannot find module 'babel-eslint' in create-react-app

Trying to install eslint into create-react-app , but get next error when running linter:

在此处输入图像描述]

Here is my .eslintrc config file:

{
  "extends": ["airbnb", "prettier", "prettier/react"],
  "plugins": ["prettier"],
  "parser": "babel-eslint"
}

If install babel-eslint manually it'll potentially produce another error based on package conflict between project and react-scripts dependencies: 在此处输入图像描述

To fix this issue just reuse babel-eslint dependency from react-scripts , that already installed. Update your config:

{
  "extends": ["airbnb", "prettier", "prettier/react"],
  "plugins": ["prettier"],
  "parser": "react-scripts/node_modules/babel-eslint"
}

Did you install @babel/eslint-parser or eslint-parser ? In my case I had to use @babel/eslint-parser and .eslintrc looks like this:

"parser": "@babel/eslint-parser",

在我的情况下,解决方案只是运行npm install eslint --save-dev来更新 eslint 版本

yarn add eslint --save-dev为我解决了这个问题!

A little late here but thought I would share what got me going...

I completely dismissed the error output which tells me where the .eslintrc file (that is looking for said package) lives. As you can see... I had some random .eslintrc living outside of my project which was somehow getting picked up.

Failed to load parser 'babel-eslint' declared in '../.eslintrc': Cannot find module 'babel-eslint'

Solution:

Deleting this package ended up fixing the error for me. Not sure how that file got there but by mistake in a previous project.

I suspect that it has something to do with installing babel-eslint and eslint globally.

For me, it is because that dependency is really not installed... I just followed the GatsbyJS's official guide, and it is not installed (not sure why that guide is not complete).

So just: yarn add -D babel-eslint

❯ yarn add -D babel-eslint 
yarn add v1.22.15
[1/4] Resolving packages...
warning babel-eslint@10.1.0: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.

babel-eslint seems deprecated and package is now provided as an ES module under babel , so remove babel-eslint and instead install @babel/eslint-parser

yarn remove babel-eslint
yarn add -D @babel/eslint-parser

Running eslint on your projects root folder eslint . will display the missing packages that you might need to install and that worked well for me.

只需在 .eslintrc 中添加@babel/eslint-parser

至于我,我只需安装这个 npm install eslint@4.x babel-eslint@8 - g 它对我有用

I have yet another "this is what worked for me" answer. Like others my issue is definitely related to the deprecation of babel-eslint . My specific issue was that eslint-config-react-app was marked as a direct dependency, which I hadn't upgraded as part of upgrading a major version of react-scripts , somehow this left me with a version of eslint-config-react-app that was expecting babel-eslint , but only @babel/eslint-parser installed. Removing eslint-config-react-app only removed the old direct dependency, but left things broken in a different way, reinstalling (without making it a direct dependency of my package) got things working again for me. I'm sure there's a cleaner way of doing this.

$npm ls eslint-config-react-app
client@0.1.1 /srv/
├── eslint-config-react-app@6.0.0
└─┬ react-scripts@5.0.1
  └── eslint-config-react-app@7.0.1

$ npm rm eslint-config-react-app

removed 1 package, and audited 2909 packages in 4s

$ npm ls eslint-config-react-app
client@0.1.1 /srv/
└─┬ react-scripts@5.0.1
  └── eslint-config-react-app@7.0.1
$ npm i --no-save eslint-config-react-app

added 1 package, removed 1 package, and audited 2909 packages in 6s

$ npm ls eslint-config-react-app
client@0.1.1 /srv/
└─┬ react-scripts@5.0.1
  └── eslint-config-react-app@7.0.1

This works like Charm

npm update eslint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM