为什么 create-react-app 在尝试 eslint 时会给出 typescript 错误(尽管没有 typescript 文件)?

Mic*_*ant 2 reactjs eslint create-react-app

当我这样做时,我收到此错误eslint .

\n
Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.yml \xc2\xbb \neslint-config-react-app#overrides[0]': Cannot find module 'typescript'\n
Run Code Online (Sandbox Code Playgroud)\n

jon*_*rpe 5

因为存在TypeScript文件 - 虽然 ESLintnode_modules/默认情况下排除 linting,但 CRA 的覆盖(为了为任何 TypeScript 文件加载适当的插件,这就是您eslint-config-react-app#overrides[0]在输出中看到的原因)确实包含node_modules/(参见例如此问题)的内容。

可以安装 TypeScript,但如果您不打算在自己的源代码中使用任何 TypeScript 文件,您可以更具体地说明您想要检查的内容。任何一个:

  1. 将脚本更改为package.json仅对源目录进行 lint 检查:

    "lint": "eslint src/"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建一个.eslintignore(或另一个文件,如果设置正确的话--ignore-path)包含以下内容的文件:

    node_modules/
    
    Run Code Online (Sandbox Code Playgroud)
  3. 添加到 ESLint 配置对象中package.json(或将其作为 传递--ignore-pattern):

    "eslintConfig": {
      "extends": "react-app",
      "ignorePatterns": [
        "node_modules/"
      ]
    }
    
    Run Code Online (Sandbox Code Playgroud)

后两个选项来自配置文档