首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.eslintrc.js而不使用.eslintrc.json

使用.eslintrc.js而不使用.eslintrc.json
EN

Stack Overflow用户
提问于 2022-01-15 08:20:59
回答 1查看 4.4K关注 0票数 0

下面是JSON:

代码语言:javascript
复制
{
    "env": {
        "node": true
    },
    "root": true,
    "parser": "@typescript-eslint/parser",
    "extends": [
        "standard",
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:import/errors",
        "plugin:import/warnings",
        "plugin:promise/recommended",
        "prettier"
    ],
    "parserOptions": {
        "ecmaVersion": 2021,
        "sourceType": "module"
    },
    "plugins": [
        "prettier", "@typescript-eslint"
    ],
    "rules": {
        "semi": [2, "always"]
    }
}

接下来是js文件:

代码语言:javascript
复制
module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  extends: [
    'standard',
    'eslint:recommended',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:import/errors',
    'plugin:import/warnings',
    'plugin:promise/recommended',
    'prettier',
  ],
  parserOptions: {
    ecmaVersion: 2021,
    sourceType: 'module',
  },
  plugins: ['prettier', '@typescript-eslint'],
  rules: {
    'semi': [2, 'always']
  },
  env: {
    node: true,
  },
};

不知道是什么导致了这个问题。

如果我注释js文件的内容并离开json,然后运行npx eslint src/test.ts,我得到以下结果:

代码语言:javascript
复制
Oops! Something went wrong! :(

ESLint: 7.32.0

ESLint couldn't find the plugin "@typescript-eslint/eslint-plugin".

(The package "@typescript-eslint/eslint-plugin" was not found when loaded as a Node module from the directory "/Users/Albert/Documents/Projects".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

    npm install @typescript-eslint/eslint-plugin@latest --save-dev

The plugin "@typescript-eslint/eslint-plugin" was referenced from the config file in "../.eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

为什么它要在'Projects‘dir中寻找插件?

我尝试了提出的解决方案,但没有奏效。

EN

回答 1

Stack Overflow用户

发布于 2022-02-26 13:00:03

看起来,项目文件夹中有一个ESLint配置文件,它位于实际项目文件夹的目录结构上。

在文件夹中运行ESLint时,ESLint首先查找该文件夹中的配置文件。然后,它在父文件夹中查找--如果那里有一个配置文件,则将其与初始配置文件合并。ESLint一直在目录结构上,并合并它找到的每个配置文件,直到它到达根目录为止。

如果要将ESLint限制为项目中的配置文件,请将其添加到eslintrc.jseslintrc.json文件中:

代码语言:javascript
复制
{
  "root": true
}

这将使ESLint不再寻找这个之外的配置文件。

无论向哪个文件添加root键,都要删除另一个文件。如果一个文件夹中有多个配置文件,ESLint将只使用一个配置文件(在eslintrc.jseslintrc.json之间,它将使用前者,因为每种格式都有优先级顺序)。

您可以找到关于优先级顺序这里和ESLint如何查找配置文件这里的更多信息。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70719874

复制
相关文章

相似问题

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