Eslint没有忽略node_modules文件夹

65

在将 Babel6 升级到 7 后,我的 eslint 开始在 node_modules 中发出警告:

enter image description here

因此,据我理解,node_modules 文件夹未被忽略,这就是问题出现的原因。因此,通过阅读 eslint 文档:

https://eslint.org/docs/user-guide/configuring

我尝试将 "ignorePatterns": ["node_modules/"], 添加到 .eslintrc 文件中,但收到以下错误信息:

Module build failed: Error: ESLint configuration in /Users/vlasenkona/Desktop/gris-seqr2/ui/.eslintrc is invalid: - Unexpected top-level property "ignorePatterns".

因此,我尝试创建 .eslintignore 文件,并仅添加了 node_modules/,但警告仍然存在。如何忽略 node_modules 文件夹?我使用的软件包版本如下:

"eslint": "^5.16.0",
"babel-eslint": "^9.0.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-import-resolver-babel-module": "^5.1.2",
"eslint-loader": "1.9.0",
"eslint-plugin-flowtype": "2.39.1",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.1.0",
"eslint-plugin-react": "^7.10.0",
"eslint-plugin-react-perf": "^2.0.8",

.eslintrc 看起来像这样:

{
  "extends": [
    "airbnb",
    "plugin:react-perf/recommended"
  ],  
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true
    }   
  },  
  "env": {
    "mocha": true,
    "es6": true,
    "browser": true,
    "node": true,
    "jest": true
  },  
  "settings": {
    "import/resolver": {
      "babel-module": {}
    }   
  },  
  "rules": {
    "semi": [2, "never"],
    "no-shadow": ["error", { "allow": ["error"] }], 
    "arrow-body-style": 0,
    "func-names": 0,
    "function-paren-newline": 0,
    "max-len": 0, //["warn", 80, 2], 
    "no-console": 0,
    "no-multi-str": 0,
    "no-param-reassign": 0,
    "no-plusplus": 0,
    "brace-style": 0,
    "object-curly-newline": 0,
    "padded-blocks": 0,
    "react/jsx-first-prop-new-line": 0,
    "react/jsx-wrap-multilines": 0,
    "react/prefer-stateless-function": 0,
    "react/sort-comp": 0,  // more freedom in arranging class functions
    "import/prefer-default-export": 0,
    "react/forbid-prop-types": 0,
    "no-class-assign": 0,
    "react/jsx-filename-extension": [ 1, { "extensions": [".js", ".jsx"]} ],
    "react/require-default-props": 0,
    "react/require-optimization": 2,
    "react/jsx-max-props-per-line": 0,
    "react/jsx-no-target-blank": 0,
    "spaced-comment": 0,

    "jsx-a11y/iframe-has-title": 0,
    "jsx-a11y/tabindex-no-positive": 0,
    "jsx-a11y/click-events-have-key-events": 0,
    "jsx-a11y/anchor-is-valid": 0
  }
}

更新

我尝试将以下内容添加到新创建的.eslintignore文件中:

node_modules/*
./node_modules/**
**/node_modules/**

但是两者都不起作用。有趣的是,如果我打开 ./node_modules/draftjs-md-converter/dist/index.js 并在文件的开头和结尾加上 /*eslint-disable */,警告仍然存在,所以也许这不是一个 eslint 的问题:只是完全误导性的警告?..


.eslintignore 文件中添加 node_modules/* 来忽略所有的 node_modules 文件夹。 - Tomas Vancoillie
1
我也尝试了这个,但错误仍然存在。 - Nikita Vlasenko
尝试将 eslint 降级到版本 4.19.1,但没有成功。 - Nikita Vlasenko
我遇到了类似的问题... @NikitaVlasenko 你解决了吗? - Ypsilon
我的问题是另一个文件夹不应该与eslint一起运行,这个文件夹是一个构建文件,在.eslintignore中它的命名方式有误。通过使用 > log.txt 命令运行eslint,然后检查日志文件,我知道了这个文件夹。在我移除它或修复了 .eslintignore 文件之后,问题得到了解决! - phuwin
显示剩余5条评论
4个回答

15

在我的情况下,ESLint(v7.8.1)正确地忽略了node_modules目录,但未能忽略我的dist目录。我将其添加到.eslintignore中:

client/dist/

问题得到解决。


1
在创建React应用程序和构建目录中,对我来说也是同样的事情。谢谢! - pom421

3

编辑:我一开始不应该有第二个 node_modules 文件夹,现在只使用主文件夹就可以了


在根目录下的 .eslintignore 文件中忽略 node_modules/ 对我起作用。

node_modules/* 不行,会在对子项目进行代码检查时导致类似 "An unhandled exception occurred: Failed to load config "airbnb" to extend from." 的错误。

我们的项目结构如下所示:

root-project

  • node_modules
  • projects
    • sub-project
      • node_modules
      • package.json
      • .eslintrc.json
      • ...
  • src
  • .eslintignore
  • .eslintrc.json
  • ...

3
对我来说,问题出在我的 Node 版本上。我正在使用版本 14,而 eslint 服务器是在版本 16 上运行的。这导致 eslint 对我所有的 Node 模块进行了代码检查。
如果你正在使用 nvm,请切换到更新的 Node 版本:
nvm use 18

您可以使用 ESLint: Show Output Channel 命令在您的IDE中查看ESLint节点服务器版本。

1
我遇到了同样的问题。 解决方案: 确保你的文件“.eslintrc”在项目的根目录中,即package.json、node_modules和其他文件所在的位置。祝好运。

4
对我而言,.eslintrc已经在存放这些其他文件的文件夹中了。把它移动到我所拥有的大型项目的根目录是逻辑上错误的。 - Nikita Vlasenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接