简体   繁体   English

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

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

I am setting up a new project as described in the typescript-eslint getting started docs.我正在按照typescript-eslint入门文档中的描述设置一个新项目。 However, in my .eslintrc.js file I am getting an error:但是,在我的.eslintrc.js文件中出现错误:

'module' is not defined.eslint(no-undef) “模块”未定义。eslint(no-undef)

Now, if I remove eslint:recommended from the extends of the config, this error goes away.现在,如果我从配置的extends中删除eslint:recommended ,这个错误就会消失。 However, typical rules like debugger or const iAmUnused = true do not get picked up by ESLint, so if feels a little like whack-a-mole.然而,像debuggerconst iAmUnused = true这样的典型规则不会被 ESLint 接受,所以 if 感觉有点像打地鼠。

Why is my ESLint file being picked up when it's in the root of my project with the eslint:recommended enabled?为什么我的 ESLint 文件位于我的项目的根目录并启用了eslint:recommended I don't want to include this file in my .eslintignore because when running my eslint command, it says this file is already automatically ignored, but it's not ♂️我不想在我的.eslintignore中包含这个文件,因为在运行我的eslint命令时,它说这个文件已经被自动忽略,但它不是 ♂️

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

Looks like the env needed to be updated from: env需要从以下位置更新环境:

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

to include node: true for the module error to be resolved.包含node: true用于要解决的module错误。

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

You need to specify the environment(s) that are relevant to your project.您需要指定与您的项目相关的环境。

In this case, you would probably want to add the commonjs environment.在这种情况下,您可能想要添加commonjs环境。

I would just consider turning off the no-undef rule though, as it's a check that's already covered by TypeScript itself.不过,我只想考虑关闭no-undef规则,因为它是 TypeScript 本身已经涵盖的检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析错误:已为 @typescript-eslint/parser 设置“parserOptions.project”。 该文件与您的项目配置不匹配:.eslintrc.js - Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: .eslintrc.js 我的“.eslintrc.js”文件出错 - “解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”。 - Error with my ".eslintrc.js" file - "Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser." 使用 typescript-eslint 强制执行文件命名约定 - Enforce file naming convention with typescript-eslint eslint 失败,无法读取配置文件:/some/path/node_modules/@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended - eslint fails with Cannot read config file: /some/path/node_modules/@typescript-eslint/eslint-plugin/dist/configs/eslint-recommended 无法加载插件@typescript-eslint:找不到模块“eslint-plugin-@typescript-eslint” - Failed to load plugin @typescript-eslint: Cannot find module 'eslint-plugin-@typescript-eslint' 无法加载在 'package.json » eslint-config-react-app#overrides[0]' 中声明的解析器 '@typescript-eslint/parser':找不到模块 'typescript' - Failed to load parser '@typescript-eslint/parser' declared in 'package.json » eslint-config-react-app#overrides[0]': Cannot find module 'typescript' typescript-eslint 未使用 tsconfig - tsconfig not used by typescript-eslint eslint 缩进和@typescript-eslint/indent 冲突 - eslint indent and @typescript-eslint/indent conflict @typescript-eslint/eslint-plugin 错误:定义了“路由”但从未使用过(no-unused-vars) - @typescript-eslint/eslint-plugin error: 'Route' is defined but never used (no-unused-vars) 使用 Prettier Eslint 时找不到模块“@typescript-eslint/parser” - Cannot find module '@typescript-eslint/parser' when using Prettier Eslint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM