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

Phi*_*cks 8 typescript eslint eslintrc typescript-eslint

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

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

现在,如果我eslint:recommendedextends配置中删除,这个错误就会消失。然而,ESLint喜欢debuggerconst iAmUnused = true不接受典型的规则,所以如果感觉有点像打鼹鼠。

为什么我的 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',
      },
    },
  ],
};
Run Code Online (Sandbox Code Playgroud)

TS配置:

{
  "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"]
}

Run Code Online (Sandbox Code Playgroud)

Phi*_*cks 13

看起来env需要从以下更新:

env: {
    browser: true,
    es6: true,
    jest: true,
  },
Run Code Online (Sandbox Code Playgroud)

包括要解决node: truemodule错误。


Bra*_*her 2

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

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

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

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