我在我的Vue(Nuxt)项目中使用ESLint和VSCode。每当我保存时,我希望我的ESLint能自动运行并自动修复所有警告。
这是我在VSCode中的settings.json文件:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"vetur.format.defaultFormatter.js": "vscode-typescript",
"vetur.validation.template": false,
"vetur.completion.scaffoldSnippetSources": {},
"vetur.completion.useScaffoldSnippets": false,
"vetur.format.defaultFormatter.html": "none",
"workbench.iconTheme": "material-icon-theme",
"git.autofetch": true,
"git.defaultCloneDirectory": "",
"gitlens.views.repositories.files.layout": "list",
"editor.tabSize": 2,
"editor.detectIndentation": false,
}
这是我的 .eslintrc.js 文件:
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
extends: [
"@nuxtjs",
"plugin:nuxt/recommended",
"../.eslintrc.js"
],
rules: {
//Add custom rules for the frontend here.
//Rules that are common for shared, frontend and backend should go in the parent file
"nuxt/no-cjs-in-config": "off",
},
}
链接的../.eslintrc.js文件包含以下内容:
module.exports = {
parserOptions: {
parser: 'babel-eslint',
},
plugins: ['jest'],
rules: {
'prefer-const': 'off',
'comma-dangle': ['error', 'always-multiline'],
'prefer-template': 'error',
},
env: {
'jest/globals': true
}
}
每当我保存文件时,警告信息就会出现并且不会自动修复。
![eslint warnings](https://istack.dev59.com/HPgX9.webp)
(node:6832) UnhandledPromiseRejectionWarning: Error: Failed to load plugin 'import' declared in 'frontend\.eslintrc.js » @nuxtjs/eslint-config': Cannot find module 'eslint-plugin-import'
Require stack:
我随后运行了yarn add eslint-plugin-import
,然后再试一次,但仍返回相同的错误。