首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@typescript-eslint/eslint-plugin已安装,但在vs代码lint任务中抛出错误,并要求不存在@typescript-eslint包

@typescript-eslint/eslint-plugin已安装,但在vs代码lint任务中抛出错误,并要求不存在@typescript-eslint包
EN

Stack Overflow用户
提问于 2021-10-15 17:36:54
回答 1查看 4.8K关注 0票数 1

使用"react-scripts": "^4.0.3"样板创建项目,为了将 eslintrc.js 文件包含在“与类型记录的交互”项目中,我尝试了eslint --init,它创建了一个默认的eslintrc.js,下面是内容。

在全局中拥有eslint v. 7.32.0

eslintrc.js

代码语言:javascript
复制
module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: ['react', '@typescript-eslint'],
  rules: {},
};

但是vs代码中的es任务是抛出错误。

(节点:37609) UnhandledPromiseRejectionWarning: TypeError:未能加载‘.eslintrc.js’中声明的插件@typescript eslint: Class扩展未定义的值不是构造函数或null

package.json

代码语言:javascript
复制
"devDependencies": {
    "@types/classnames": "^2.3.1",
    "@types/react-redux": "^7.1.19",
    "@types/react-router-dom": "^5.3.1",
    "@typescript-eslint/parser": "^4.33.0",
    "eslint": "^7.32.0",
    "eslint-plugin-import": "^2.25.2",
    "typescript": "^4.4.4"
  },
 "dependencies": {
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.5",
    "react-router-dom": "^5.3.0",
    "react-scripts": "^4.0.3",
    "redux": "^4.1.1",
}

现在,当我运行下面的命令来安装缺少的包时

npm i--保存-dev @typescript-eslint/eslint-plugin

它在终端中抛出错误

代码语言:javascript
复制
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: react-app@0.1.0
npm ERR! Found: @typescript-eslint/parser@4.33.0
npm ERR! node_modules/@typescript-eslint/parser
npm ERR!   dev @typescript-eslint/parser@"^4.33.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @typescript-eslint/parser@"^5.0.0" from @typescript-eslint/eslint-plugin@5.0.0
npm ERR! node_modules/@typescript-eslint/eslint-plugin
npm ERR!   dev @typescript-eslint/eslint-plugin@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

npm ERR! A complete log of this run can be found in

因此,按照用选项尝试的建议

npm i--保存-dev @typescript-eslint/eslint-plugin -遗留-对等-deps

安装成功,但版本(5.x)高于@typescript-eslint/parser版本,如eslint-插件文档中所述

对于@typescript-eslint/解析器和@typescript-eslint/eslint,使用相同的版本号是很重要的。

所以请重新安装类似版本的软件包。

代码语言:javascript
复制
> npm install --save-dev @typescript-eslint/eslint-plugin@4.33.0

但是在vs代码中,它仍然抛出错误

UnhandledPromiseRejectionWarning: TypeError:未能加载“.eslintrc.js”中声明的插件@typescript-eslint: Class扩展未定义的值不是构造函数或null

所以我试着安装

npm安装--保存-dev @typescript-eslint

但它会抛出错误

代码语言:javascript
复制
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "@typescript-eslint": Tags may not have any characters that encodeURIComponent encodes.

也尝试通过更新es lint最新版本8.0,但它造成了另一个问题。

我知道这是因为我想安装一个不存在的软件包,但如何解决这个问题呢?我是不是漏掉了什么。

  • vscode -v1.61.1
  • ubuntu -v20.04
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-15 19:26:14

eslint插件命名约定检查之后,发现我必须按照以下语法安装缺失的@typescript-eslint

npm安装--保存-dev @typescript-eslint/eslint-plugin

这解决了问题,但新的错误来的反应版本是没有指定的,所以添加在“设置”键。

eslintrc.js

代码语言:javascript
复制
// eslint-disable-next-line no-undef
module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 12,
    sourceType: 'module',
  },
  plugins: ['react', '@typescript-eslint'],
  rules: {
    '@typescript-eslint/no-explicit-any': 'off',
    'react/react-in-jsx-scope': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
  },
  settings: {
    react: {
      version: 'latest',
    },
  },
};

package.json

代码语言:javascript
复制
"devDependencies": {
    "@types/classnames": "^2.3.1",
    "@types/react-redux": "^7.1.19",
    "@types/react-router-dom": "^5.3.1",
    "@typescript-eslint/eslint-plugin": "^4.33.0",
    "@typescript-eslint/parser": "^4.33.0",
    "eslint": "^7.32.0",
    "eslint-plugin-import": "^2.25.2",
    "redux-devtools": "^3.7.0",
    "redux-devtools-extension": "^2.13.9",
    "typescript": "^4.4.4"
  }

tsconnfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "noImplicitAny": false,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "downlevelIteration": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69588594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档