简体   繁体   English

错误:找不到模块“eslint/lib/rules/no-unused-expressions”

[英]Error: Cannot find module 'eslint/lib/rules/no-unused-expressions'

After installing ESLint through NPM, I'm getting the following error in my browser:通过 NPM 安装 ESLint 后,我​​在浏览器中收到以下错误:

Error: Cannot find module 'eslint/lib/rules/no-unused-expressions' Referenced from:错误:找不到模块 'eslint/lib/rules/no-unused-expressions' 引用自:

Any idea what could cause this and how I can fix it?知道什么可能导致这种情况以及我该如何解决吗?

Try these steps:尝试以下步骤:

  1. Delete package-lock.json file删除 package-lock.json 文件
  2. Delete node_modules folder删除 node_modules 文件夹
  3. Delete .eslintrc file删除 .eslintrc 文件
  4. Remove devDependencies that contains 'eslint'删除包含 'eslint' 的 devDependencies
  5. In the terminal type: npm install在终端输入:npm install
  6. In the terminal type: npm run start在终端输入:npm run start

In case you want to have eslint, try checking if you have in your devDependencies the line below:如果您想要 eslint,请尝试检查您的 devDependencies 中是否有以下行:

"eslint": "5.6.0"
  1. NPM INSTALL ESLINT NPM 安装 ESLINT

I had this issue while using a create-react-app.我在使用 create-react-app 时遇到了这个问题。 React scripts already come with an eslint version installed and that means you shouldn't need to install eslint on your own. React 脚本已经安装了 eslint 版本,这意味着您不需要自己安装 eslint。 I fixed this issue by doing the following:我通过执行以下操作解决了这个问题:

  1. Deleted node-modules folder删除了 node-modules 文件夹
  2. Deleted my package-lock.json file删除了我的 package-lock.json 文件
  3. Removed my local installation of eslint from the package.json file从 package.json 文件中删除了我本地安装的 eslint
  4. Installed all packages again npm install再次安装所有软件包npm install
  5. npm start and everything was working fine again npm start一切正常

Deleted node-modules folder删除了 node-modules 文件夹
Deleted my package-lock.json file删除了我的 package-lock.json 文件
Removed eslint from the package.json file从 package.json 文件中删除了 eslint
Installed all packages again再次安装所有软件包

npm install   
npm start 

and everything was working fine again一切都恢复正常了

Easy fix #1轻松修复 #1

npm i -D --save eslint-loader

Easy fix #2轻松修复 #2

npm i -D --save eslint@5.16.0

Explanation :说明

  • If you look at the error stack trace you'll see the error is actually fired from eslint-loader/index.js (so not eslint ).如果您查看错误堆栈跟踪,您会看到错误实际上是从eslint-loader/index.js触发的(所以不是eslint )。
  • Then in node_modules, you can find eslint-loader and check the version in its package.json .然后在 node_modules 中,你可以找到eslint-loader并在它的package.json检查版本。 It's probably quite outdated.它可能已经过时了。 Mine was at 1.9.0, while the current version of eslint-loader is 4.0.2我的是 1.9.0,而eslint-loader的当前版本是 4.0.2
  • And back to the error - that path that it is complaining about apparently did get changed from eslint/lib/formatters/stylish to eslint/lib/formatters/<some-other-folder>/stylish in a newer version of eslint (I think v6).而回的错误-这条道路,它是抱怨显然没有得到更改从eslint/lib/formatters/stylisheslint/lib/formatters/<some-other-folder>/stylish在eslint的新版本(我认为v6)。 And now this older eslint-loader version is failing to find the updated path in eslint.现在这个旧的 eslint-loader 版本无法在 eslint 中找到更新的路径。

So either upgrading eslint-loader to a newer version that knows this new path (Easy fix 1), or downgrading eslint down to when it had that old path (Easy fix 2), should both work.因此,要么将eslint-loader升级到知道这个新路径的新版本(Easy fix 1),要么将eslint降级到它有旧路径时(Easy fix 2),两者都应该有效。

For the record I think this outdated dependency is coming from webpack, which makes it difficult to debug for the create-react-app folks.作为记录,我认为这个过时的依赖来自 webpack,这使得 create-react-app 人员很难调试。

Make sure you clean your node_modules .确保清理node_modules Delete your package-lock.json.删除您的 package-lock.json。 and run the并运行

npm cache clean --force to clean the cache. npm cache clean --force清理缓存。 Now install the eslint by running below in the directory:现在通过在目录中运行以下来安装eslint

npm i --save eslint

I know it's late to answer, maybe it's gonna help someone who comes to this thread.我知道现在回答已经晚了,也许它会帮助来到这个线程的人。

for linux user and using npx just type npx eslint --init as the documentation says对于 linux 用户并使用npx只需键入npx eslint --init文档所述

你应该更新 node.js 和 npm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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