![](/img/trans.png)
[英]Why Eslint Plugins is found locally but I cannot build on the server?
[英]Why eslint plugins are not applied when I am trying to use eslint nodejs api?
我想在我的節點js腳本中使用eslint api。 我發現了這一點: https : //eslint.org/docs/developer-guide/nodejs-api 。 所以我正在嘗試創建CLIEngine,加載配置並使用API進行處理。
當我嘗試使用API Definition for rule <ruleName> was not found.
某些代碼時,出現錯誤: Definition for rule <ruleName> was not found.
。
您可以在此倉庫中查看代碼: https : //github.com/sharikovvladislav/eslint-try-api-with-plugins
這是我的全部代碼:
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"]
為什么Definition for rule \\'node/no-extraneous-require\\' was not found.
錯誤(以及其他類似錯誤)? 我缺少什么?
要點是根本不使用棉絨/棉絨。
答案在這里: https : //github.com/eslint/eslint/issues/12030#issuecomment-516214604
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): https : //github.com/sharikovvladislav/eslint-try-api-with-plugins/pull/1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.