【问题标题】:Why is create-react-app giving typescript error when trying to eslint (though no typescript files)?为什么 create-react-app 在尝试 eslint 时会给出打字稿错误(尽管没有打字稿文件)?
【发布时间】:2020-11-05 08:25:46
【问题描述】:

当我执行eslint .时出现此错误

Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.yml » 
eslint-config-react-app#overrides[0]': Cannot find module 'typescript'

【问题讨论】:

    标签: reactjs create-react-app eslint


    【解决方案1】:

    安装打字稿

    npm install --save-dev typescript
    

    然后eslint .

    将按预期工作并且可以使用.eslintrc.yml 文件。

    顺便说一句,我也刚刚安装了

    npm install -save-save eslint-plugin-typescript
    

    如果您正在这样做,您可能也需要这样做。

    顺便说一句,不要使用 npm install eslint... 安装 eslint,因为它可能会因为...crewate-react-app 已经拥有它(但版本较低)而搞砸。因此,只需添加您的 eslintrc.yml 文件,它就会起作用(在编辑器中应用,并根据需要在命令行中使用此修复程序)。

    示例输出:

    .../src/components/SignUp/index.js
       1:41  error  Missing semicolon                              semi
       7:26  error  Unnecessary parentheses around expression      no-extra-parens
      15:2   error  Missing semicolon                              semi
      32:13  error  'username' is assigned a value but never used  no-unused-vars
      47:63  error  Missing semicolon                              semi
      95:26  error  Unnecessary parentheses around expression      no-extra-parens
    

    【讨论】:

      【解决方案2】:

      因为那里有 TypeScript 文件 - 尽管 ESLint 默认排除 node_modules/ 进行 linting,但 CRA 的 override (为了为任何 TypeScript 文件加载适当的插件,这就是您看到 @987654326 的原因@ 在输出中)确实包含了node_modules/ 的内容(参见例如this issue)。

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

      1. package.json 中的脚本更改为仅对源目录进行 lint:

        "lint": "eslint src/"
        
      2. 创建一个.eslintignore(或另一个文件,如果您设置了正确的--ignore-path)文件,其中包含:

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

        "eslintConfig": {
          "extends": "react-app",
          "ignorePatterns": [
            "node_modules/"
          ]
        }
        

      后两个选项来自the documentation on configuration

      【讨论】:

      • @DmitryGrinko 有没有机会在此基础上进行一些扩展?这三个选项都不会对结果产生任何影响吗?其中一个或多个会导致不同的结果,这仍然不是您所希望的?
      • @DmitryGrinko 这似乎与问题中提到的错误不同,据我所知,CRA 不使用eslint-config-airbnb-typescript。如果您使用的是其他模板,那将完全是一个单独的问题。
      • 我没有使用 CRA。我正在尝试在 SLS+TS 上安装 eslint
      • @DmitryGrinko 我不知道SLS+TS是什么,但是这个问题是关于CRA内置ESLint配置引起的问题。如果您不使用它,那么我不一定希望这些答案有效,因为这可能是一个不相关的问题。
      • @DmitryGrinko 但如果您实际上使用的是 TS,那么应该已经安装了相关的依赖项,您根本看不到这一点。 eslint-config-airbnb-typescript 通过@typescript-eslint/eslint-plugin@typescript-eslint/parsertypescript 上有一个对等依赖。这个问题是关于没有安装 TypeScript 的情况,因为它不需要 - OP 的源是 JS。您的问题似乎无关,我建议您提出一个单独的问题(并检查您是否安装了所有正确的对等部门 - npm ls,请参阅 stackoverflow.com/a/63177495/3001761)。
      猜你喜欢
      • 2020-02-12
      • 1970-01-01
      • 2018-07-27
      • 2020-09-17
      • 1970-01-01
      • 2019-11-11
      • 2020-08-27
      • 2017-05-06
      • 1970-01-01
      相关资源
      最近更新 更多