11

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

'module' 没有定义。eslint(no-undef)

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

为什么我的 ESLint 文件在我的项目的根目录中且启用时会被拾取eslint:recommended?我不想在我的文件中包含这个文件,.eslintignore因为在运行我的eslint命令时,它说这个文件已经被自动忽略了,但它不是 ‍♂️</p>

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',
      },
    },
  ],
};

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

4

2 回答 2

19

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

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

包括要解决node: truemodule错误。

于 2020-08-20T19:49:53.123 回答
0

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

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

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

不过,我会考虑关闭no-undef规则,因为它是 TypeScript 本身已经涵盖的检查。

于 2020-08-20T05:51:29.217 回答