eslint 失败,无法读取配置文件:/some/path/node_modules/@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended

Mar*_*tus 3 typescript eslint webpack

我正在按照说明从这里设置 ESLint 。

ESLint 失败并显示以下消息:

$ npm run lint

> IACS@1.0.0 lint /some/path/sscce-typescript-react-eslint
> eslint '*/**/*.{js,ts,tsx}' --quiet --fix

Error: Cannot read config file: /some/path/sscce-typescript-react- 
eslint/node_modules/@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended
Error: ENOENT: no such file or directory, open '/some/path/sscce-typescript-react- 
eslint/node_modules/@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended'
Referenced from: /some/path/sscce-typescript-react-eslint/node_modules/@typescript- 
eslint/eslint-plugin/dist/index.js
Referenced from: /some/path/sscce-typescript-react-eslint/.eslintrc.js
Run Code Online (Sandbox Code Playgroud)

确实存在具有以下名称的文件:

/some/path/sscce-typescript-react-eslint/node_modules/@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended.js
Run Code Online (Sandbox Code Playgroud)

...但显然 ESLint 没有预料到*.js扩展名并且找不到该文件。

这个 github repo 中找到了最短的独立具体示例。

为了本网站的完整性,我package.json是:

{
  "name": "IACS",
  "version": "1.0.0",
  "description": "react, typescript",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --hot --inline --devtool source-map --progress --colors  --port 8090",
    "lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.8.7",
    "@babel/plugin-proposal-class-properties": "^7.8.7",
    "@babel/plugin-transform-async-to-generator": "^7.8.7",
    "@babel/preset-env": "^7.8.7",
    "@babel/preset-react": "^7.8.7",
    "@babel/register": "^7.8.7",
    "@babel/runtime": "^7.8.7",
    "@types/jquery": "^3.5.1",
    "@types/react": "^16.9.40",
    "@types/react-dom": "^16.9.8",
    "@typescript-eslint/eslint-plugin": "^3.7.1",
    "@typescript-eslint/parser": "^3.7.1",
    "babel-loader": "^8.0.5",
    "cross-env": "^5.2.0",
    "css-loader": "^2.1.0",
    "eslint": "^5.16.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-plugin-import": "^2.15.0",
    "eslint-plugin-jsx-a11y": "^6.2.0",
    "eslint-plugin-react": "^7.20.5",
    "file-loader": "^3.0.1",
    "html-webpack-plugin": "^3.2.0",
    "null-loader": "^0.1.1",
    "react-hot-loader": "^4.6.3",
    "ts-loader": "^7.0.5",
    "typescript": "^3.9.5",
    "webpack": "^4.29.0",
    "webpack-cli": "^3.3.*",
    "webpack-dev-server": "^3.1.14"
  },
  "dependencies": {
    "@babel/polyfill": "^7.8.7",
    "jquery": "^3.5.1",
    "react": "^16.8.0",
    "react-dom": "^16.9.8"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的.eslintrc.js是:

module.exports = {
    parser: "@typescript-eslint/parser",
    "plugins": ["@typescript-eslint"],
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true
    }
  },
  settings: {
    react: {
      version: "detect"
    }
  },
  extends: [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  rules: {
      "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"]
  },
};
Run Code Online (Sandbox Code Playgroud)

Pau*_*erg 6

就我而言,问题是我最近更新了 VSCode 的版本eslint,并且 VSCode 仍在尝试加载旧版本(不知道为什么)。我通过重新加载窗口修复了这个问题:

  1. SHIFT+CMD+P
  2. 单击“开发人员:重新加载窗口”


小智 5

我最近在 windows 下遇到了同样的问题,并将eslintpackage.json 中的依赖更新为

"eslint": "^7.6.0"
Run Code Online (Sandbox Code Playgroud)

解决了这个问题。我希望这对您来说也是一个可行的解决方案。