簡體   English   中英

typescript-eslint 配置:.eslintrc 文件“模塊”未定義

[英]typescript-eslint config: .eslintrc file 'module' is not defined

我正在按照typescript-eslint入門文檔中的描述設置一個新項目。 但是,在我的.eslintrc.js文件中出現錯誤:

“模塊”未定義。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',
      },
    },
  ],
};

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

env需要從以下位置更新環境:

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

包含node: true用於要解決的module錯誤。

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

您需要指定與您的項目相關的環境。

在這種情況下,您可能想要添加commonjs環境。

不過,我只想考慮關閉no-undef規則,因為它是 TypeScript 本身已經涵蓋的檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM