问题描述
我正在按照typescript-eslint入门文档中的说明设置一个新项目。但是,在我的.eslintrc.js
文件中,出现错误:
'module'未定义。eslint(no-undef)
现在,如果我从配置的eslint:recommended
中删除extends
,此错误就消失了。但是,ESLint不会接受debugger
或const iAmUnused = true
之类的典型规则,因此感觉有点像rules子。
当我的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"]
}
解决方法
看起来像env
的需要从以下位置进行更新:
env: {
browser: true,es6: true,jest: true,},
包含node: true
,以解决module
错误。
https://eslint.org/docs/user-guide/configuring#specifying-environments
您需要指定与您的项目相关的环境。
在这种情况下,您可能想要添加commonjs
环境。
我只是考虑关闭no-undef
规则,因为TypeScript本身已经涵盖了这项检查。