Node.js 为什么在我尝试使用eslint nodejs api时不应用eslint插件?

Node.js 为什么在我尝试使用eslint nodejs api时不应用eslint插件?,node.js,eslint,Node.js,Eslint,我想在我的节点js脚本中使用eslint api。我找到了这个:。所以我尝试使用API创建CLIEngine、加载配置和lint 当我试图使用API过滤某些代码时,我遇到了错误:未找到规则的定义。 您可以查看此回购协议中的代码: 以下是我的全部代码: const CLIEngine = require('eslint').CLIEngine; const Linter = require('eslint').Linter; const path = require('path'); const

我想在我的节点js脚本中使用eslint api。我找到了这个:。所以我尝试使用API创建CLIEngine、加载配置和lint

当我试图使用API过滤某些代码时,我遇到了错误:
未找到规则的定义。

您可以查看此回购协议中的代码:

以下是我的全部代码:

const CLIEngine = require('eslint').CLIEngine;
const Linter = require('eslint').Linter;
const path = require('path');

const cli = new CLIEngine({
    configPath: path.resolve(process.cwd(), '.eslintrc.json')
});

const linter = new Linter();

const result = linter.verifyAndFix('var foo', cli.getConfigForFile("script.js"));

console.log(result);
结果我得到了如下结果:

 ~/tmp/test-eslint-with-plugins  node script.js
{ fixed: false,
  messages:
   [ { ruleId: 'node/no-extraneous-require',
       message:
        'Definition for rule \'node/no-extraneous-require\' was not found.',
       line: 1,
       column: 1,
       endLine: 1,
       endColumn: 2,
       severity: 2,
       nodeType: null }
  // ....
]
}
我的包依赖项中有以下内容:

    "eslint": "^6.1.0",
    "eslint-plugin-node": "^9.1.0"
这是我的eslintrc:

{
    "extends": [
        "eslint:recommended",
        "plugin:node/recommended"
    ],
    "rules": {
        "node/exports-style": ["error", "module.exports"],
        "node/file-extension-in-import": ["error", "always"],
        "node/prefer-global/buffer": ["error", "always"],
        "node/prefer-global/console": ["error", "always"],
        "node/prefer-global/process": ["error", "always"],
        "node/prefer-global/url-search-params": ["error", "always"],
        "node/prefer-global/url": ["error", "always"],
        "node/prefer-promises/dns": "error",
        "node/prefer-promises/fs": "error"
    }
}
所以我指定了一些插件和规则。看起来一切都是对的

我试着调试一下,发现CLI是正常创建的(看起来很像)。此外,配置文件已正确解析。我可以在配置属性中看到这一点:

cli.getConfigForFile("script.js").plugins
>> ["node"]
Object.keys(cli.getConfigForFile("script.js").rules).filter(key => key.startsWith('node'))
>> ["node/exports-style", "node/file-extension-in-import", "node/prefer-global/buffer", "node/prefer-global/console", "node/prefer-global/process", "node/prefer-global/url-search-params", "node/prefer-global/url", "node/prefer-promises/dns", "node/prefer-promises/fs", "node/no-deprecated-api", "node/no-extraneous-require", "node/no-missing-require", "node/no-unpublished-bin", "node/no-unpublished-require", "node/no-unsupported-features/es-builtins", "node/no-unsupported-features/es-syntax", "node/no-unsupported-features/node-builtins", "node/process-exit-as-throw", "node/shebang", "node/no-extraneous-import", "node/no-missing-import", "node/no-unpublished-import"]

为什么未找到规则“节点/无额外要求”的
定义。
错误(以及类似的其他错误)?我遗漏了什么?

主要的一点是根本不使用棉绒/棉绒

答案如下:

Linter类是为在浏览器中工作而设计的,所以它不依赖于任何文件系统。这意味着Linter找不到/加载任何插件、解析器和可共享配置。您必须通过linter.defineRule方法或类似方法手动准备这些

以下是完整代码:

const cli = new CLIEngine({
    configPath: path.resolve(process.cwd(), '.eslintrc.json'),
    // eslint --fix *
    fix: true
});

// run lint & get output for fix
const report = cli.executeOnFiles([path.resolve(process.cwd(), 'test.js')]);

// log about report
console.log('>> report', report);

// write fixed files to fs
CLIEngine.outputFixes(report);

下面是关于如何在我的案例(github)中修复它的完整代码:

好吧,看起来这种方法不起作用。我用了另一种方法。我调试了一下命令行界面,发现使用了另一种方法。这是全部信息