【问题标题】:Typescript eslint - Missing file extension "ts" import/extensionsTypescript eslint - 缺少文件扩展名“ts”导入/扩展
【发布时间】:2020-04-03 13:35:11
【问题描述】:

我有一个使用 Typescript 制作的简单 Node/Express 应用程序。 并且 eslint 给我错误

Missing file extension "ts" for "./lib/env" import/extensions

这是我的 .eslintrc 文件

    {
  "extends": [
    "airbnb",
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/react",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript"
  ],
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier", "import"],
  "settings": {
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "typescript": {
        "directory": "./tsconfig.json"
      },
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },
  "rules": {
    "@typescript-eslint/indent": [2, 2],
    "no-console": "off",
    "import/no-unresolved": [2, { "commonjs": true, "amd": true }],
    "import/named": 2,
    "import/namespace": 2,
    "import/default": 2,
    "import/export": 2
  }
}

我已经安装了 eslint-plugin-import 和 eslint-import-resolver-typescript。 我不知道为什么,我得到了那个错误。

【问题讨论】:

标签: node.js typescript eslint


【解决方案1】:

将以下代码添加到rules

"rules": {
   "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never",
        "ts": "never",
        "tsx": "never"
      }
   ]
}

airbnb ESLint config 导致问题。

【讨论】:

  • 成功了!谢谢。 (其他读者:确保将其放在“规则”而不是“设置”下......)
  • @Venryx 感谢您的善意提醒。我只是把它放在错误的块中。
  • 但这有什么作用呢?
  • @JulianSoto 对于在打字稿项目中使用“eslint-config-airbnb-base”的人来说,有一条规则是“确保在导入路径中一致使用文件扩展名”,这会导致问题。所以我们可以覆盖它以适应打字稿项目。
  • 使用 eslint 7.27.0 我收到此错误Configuration for rule "import/extensions" is invalid: Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '"error"')
【解决方案2】:

我知道这已经晚了,但如果您要扩展 Airbnb 规则,您可以使用eslint-config-airbnb-typescript。请参阅项目页面以获取最新说明。以下是截至 2021 年 10 月的要点:

安装

npm install -D eslint-config-airbnb-typescript

ESLint 配置文件

使用 React - 在“airbnb”之后添加“airbnb-typescript”

extends: [
  'airbnb',
+ 'airbnb-typescript'
]

没有 React - 在“airbnb-base”之后添加“airbnb-typescript/base”

extends: [
  'airbnb-base',
+ 'airbnb-typescript/base'
]

parserOptions.project 设置为tsconfig.json的路径

{
  extends: ['airbnb', 'airbnb-typescript'],
+ parserOptions: {
+   project: './tsconfig.json'
+ }
}

注意:在 2021 年 8 月之前,您还需要完成以下任务。 这些说明保留在这里仅供历史参考。您应该不再需要这样做了。

先卸载旧的,再添加新的:

# uninstall whichever one you're using
npm uninstall eslint-config-airbnb
npm uninstall eslint-config-airbnb-base

# install the typescript one
npm install -D eslint-config-airbnb-typescript

然后更新您的 ESlint 配置,从“扩展”部分删除旧的 Airbnb 并添加新的:

extends: ["airbnb-typescript"]

【讨论】:

  • 谢谢兄弟。这是此问题的最佳解决方案,因为正如公认的答案所述,问题来自没有 typescript 支持的常规 airbnb 配置。这个包只是增加了支持。
  • 这应该是公认的答案;其他的都是创可贴,这是真正的问题。
  • 根据最新的说明你不应该uninstall eslint-config-airbnb请看:npmjs.com/package/eslint-config-airbnb-typescript
  • 谢谢!我已更新答案以反映新流程。
  • 谢谢!运行 npm install -D eslint-config-airbnb-typescript 并添加 'airbnb-typescript' 为我修复。
【解决方案3】:

通过捆绑我在互联网上找到的所有答案来解决这个问题:

这是我的最终结果:

{
  "settings": {
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },

  "env": {
    "browser": true,
    "es2021": true
  },

  "extends": ["plugin:react/recommended", "airbnb"],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 12,
    "sourceType": "module"
  },
  "plugins": ["import", "react", "@typescript-eslint"],
  "rules": {
    "no-console": 1,
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never",
        "ts": "never",
        "tsx": "never",
        "mjs": "never"
      }
    ]
  }
}

我正在使用 react native 顺便说一句。如果您使用不同的东西,只需删除与反应相关的东西就足够了

【讨论】:

    【解决方案4】:

    如果您像我一样尝试配置 babel、旧版 Javascript 和新的打字稿文件并尝试在 *.ts 上启用无扩展名导入,那么我遇到了 this ESLint issue(我还找到了以下解决方案):

    除了rules 配置:

    "rules": {
       "import/extensions": [
          "error",
          "ignorePackages",
          {
            "js": "never",
            "mjs": "never",
            "jsx": "never",
            "ts": "never",
            "tsx": "never"
          }
       ]
    }
    

    您还需要在.eslintrc.js 中添加额外的设置条目:

      "settings": {
        "import/extensions": [".js", ".mjs", ".jsx", ".ts", ".tsx"]
      },
    

    祝你好运!

    【讨论】:

      【解决方案5】:

      .eslintrc

      添加导入扩展和解析器很重要
          "settings": {
          "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
          "import/parsers": {
            "@typescript-eslint/parser": [".ts", ".tsx"]
          },
          "import/resolver": {
            "node": {
              "extensions": [".js", ".jsx", ".ts", ".tsx"]
            }
          }
        }
      },
      

      进一步添加规则:

       "import/extensions": [
        "error",
        "ignorePackages",
        {
          "js": "never",
          "jsx": "never",
          "ts": "never",
          "tsx": "never"
        }
      ]
      
      
      
      enter code here
      

      【讨论】:

        猜你喜欢
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        • 2016-03-01
        • 2019-10-05
        相关资源
        最近更新 更多