无法加载在“.eslintrc.cjs”中声明的插件“@typescript-eslint”:意外的标记“。”

Kar*_*ind 4 webstorm typescript-eslint

我\xe2\x80\x99m 使用 TypeScript 和 ESLint 开发 React 项目。我有以下版本的eslinttypescript-eslint我的 package.json 文件中

\n
"@typescript-eslint/eslint-plugin": "^6.3.0",\n"@typescript-eslint/parser": "^6.0.0",\n"eslint": "^8.45.0",\n
Run Code Online (Sandbox Code Playgroud)\n

我也有以下.eslintrc.cjs文件:

\n
/* eslint-env node */\n\nmodule.exports = {\n  root: true,\n  env: { browser: true, es2020: true },\n  extends: [\n    \'eslint:recommended\',\n    \'plugin:@typescript-eslint/recommended\',\n    \'plugin:@typescript-eslint/recommended-requiring-type-checking\',\n    \'plugin:react-hooks/recommended\',\n  ],\n  parser: \'@typescript-eslint/parser\',\n  parserOptions: {\n    ecmaVersion: \'latest\',\n    sourceType: \'module\',\n    project: true,\n    tsconfigRootDir: __dirname,\n  },\n  plugins: [\'react-refresh\'],\n  rules: {\n    \'react-refresh/only-export-components\': [\n      \'warn\',\n      { allowConstantExport: true },\n    ],\n    \'@typescript-eslint/no-non-null-assertion\': \'off\',\n    "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],\n  },\n}\n
Run Code Online (Sandbox Code Playgroud)\n

对于 WebStrom 中的每个文件,我收到以下错误:

\n
\n

SyntaxError: 无法加载在 \'.eslintrc.cjs\' 中声明的插件 \'@typescript-eslint\': 意外的标记 \'.\'

\n
\n

我尝试将我的 Node 版本更新到 20.5.1,重新安装所有依赖项,并验证我的.eslintrc.cjs文件,但似乎没有任何效果。我使用相同文件的同事没有遇到任何问题。

\n

我也尝试了这里的所有解决方案

\n

有谁知道可能导致此错误的原因以及如何修复它?任何帮助,将不胜感激。

\n

Kar*_*ind 8

终于找到解决办法了,是IDE的问题

步骤1

查找项目使用的节点版本

node --version
Run Code Online (Sandbox Code Playgroud)

第2步

对 IDE 解释器使用相同的节点版本

文件 -> 设置 -> 语言和框架 -> Node.js

在此输入图像描述

步骤3

如果问题仍然存在,您可以从 Webstorm 安装插件并从操作栏重新启动 eslint 服务

插件链接