【问题标题】:typescript-eslint config: .eslintrc file 'module' is not definedtypescript-eslint 配置:.eslintrc 文件“模块”未定义
【发布时间】:2020-12-08 04:47:10
【问题描述】:

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

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

现在,如果我从配置的 extends 中删除 eslint:recommended,此错误就会消失。然而,像 debuggerconst iAmUnused = true 这样的典型规则不会被 ESLint 接受,所以 if 感觉有点像打地鼠。

为什么我的 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"]
}

【问题讨论】:

标签: typescript eslint eslintrc typescript-eslint


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 是的,我将no-undef 包括在内用于测试目的,现在我已将其删除。谢谢!
    猜你喜欢
    • 2021-04-16
    • 2020-03-07
    • 2021-12-26
    • 1970-01-01
    • 2021-01-10
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    相关资源
    最近更新 更多