typescript-eslint配置:.eslintrc文件“模块”未定义

问题描述

我正在按照typescript-eslint入门文档中的说明设置一个新项目。但是,在我的.eslintrc.js文件中,出现错误

'module'未定义。eslint(no-undef)

现在,如果我从配置的eslint:recommended删除extends,此错误就消失了。但是,ESLint不会接受debuggerconst iAmUnused = true之类的典型规则,因此感觉有点像rules子。

当我的ESLint文件位于启用了eslint:recommended的项目的根目录中时,为什么会拾取它?我不想将此文件包含在.eslintignore中,因为运行我的eslint命令时,它说该文件已被自动忽略,但不是?‍♂️

ESlintrC:

module.exports = {
  root: true,parser: '@typescript-eslint/parser',parserOptions: {
    project: '*/tsconfig.json',},settings: {
    react: {
      version: 'detect',plugins: ['@typescript-eslint','jest','react','react-hooks'],extends: [
    'eslint:recommended','plugin:@typescript-eslint/recommended','plugin:jest/recommended','plugin:prettier/recommended','plugin:react/recommended','plugin:react-hooks/recommended','prettier','prettier/@typescript-eslint',],rules: {
    'no-unused-vars': 2,env: {
    browser: true,es6: true,jest: true,overrides: [
    {
      files: ['**/*.tsx'],rules: {
        'react/prop-types': 'off',};

TSConfig

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,"esModuleInterop": true,"declaration": true,"declarationDir": "build","jsx": "react","lib": ["es6","dom","es2016","es2017"],"module": "esnext","moduleResolution": "node","noEmit": true,"resolveJsonModule": true,"rootDir": "./src","rootDirs": ["./src"],"sourceMap": true,"strict": true,"target": "es5"
  },"include": ["./src"],"exclude": ["node_modules","build","dist","src/**/*.stories.tsx","src/**/*.test.tsx"]
}

解决方法

看起来像env的需要从以下位置进行更新:

env: {
    browser: true,es6: true,jest: true,},

包含node: true,以解决module错误。

,

https://eslint.org/docs/user-guide/configuring#specifying-environments

您需要指定与您的项目相关的环境。

在这种情况下,您可能想要添加commonjs环境。

我只是考虑关闭no-undef规则,因为TypeScript本身已经涵盖了这项检查。