[英]Extension 'eslint' is configured as formatter but it cannot format 'JavaScript'-files
I am experiencing an intermittent problem with ESLint in VS Code.我在 VS Code 中遇到 ESLint 间歇性问题。 When saving a file, instead of formatting it, this message shows in the status bar:
保存文件而不是格式化文件时,此消息会显示在状态栏中:
Apparently no one has ever reported this message on Google.显然没有人在 Google 上报告过此消息。
When I say it is intermittent, it was working fine, then the computer (MBP) crashed, and on restarting this is the situation.当我说它是间歇性的时,它工作正常,然后计算机 (MBP) 崩溃了,重新启动时就是这种情况。 It has happened previously, but I don't recall what I did to fix it.
它以前发生过,但我不记得我做了什么来修复它。
The app is a fairly complex Vue-based app based on a pre-configured template.该应用程序是一个基于预配置模板的相当复杂的基于 Vue 的应用程序。 In package.json:
在 package.json 中:
"@vue/cli-plugin-eslint": "^4.5.13",
"babel-eslint": "^10.1.0",
"eslint": "^7.31.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-vue": "^7.14.0",
In the VS Code workspace file:在 VS Code 工作区文件中:
"editor.codeLens": true,
"eslint.format.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
},
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[vue]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"eslint.validate": [
"javascript"
],
"debug.javascript.usePreview": true,
"debug.javascript.usePreviewAutoAttach": true,
"[jsonc]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
I'm really not sure how to proceed to understand why vscode-eslint is reporting that it can't format JavaScript files.我真的不确定如何继续理解为什么 vscode-eslint 报告它无法格式化 JavaScript 文件。
EDIT编辑
This issue regarding TypeScript gives some hints. This issue regarding TypeScript给出了一些提示。 It seems this error can result when the ESLint server is restarting or the extension host is under a lot of stress.
当 ESLint 服务器重新启动或扩展主机承受很大压力时,似乎会导致此错误。
In my project now, most files are formatting fine, although one consistently fails.在我现在的项目中,大多数文件的格式都很好,尽管有一个一直失败。 That file is called
eslint.js
and is part of a build process, running eslint - I don't totally understand the configuration.该文件称为
eslint.js
并且是构建过程的一部分,运行 eslint - 我不完全理解配置。 I suspect that saving this file somehow causes eslint to be run/reloaded, and while that is happening, eslint-vscode tries to also run it and fails.我怀疑以某种方式保存此文件会导致 eslint 运行/重新加载,并且在发生这种情况时,eslint-vscode 也尝试运行它但失败了。
I was actually experiencing the same problem and I only needed to remove the .eslintignore
file, which only contained this line:我实际上遇到了同样的问题,我只需要删除
.eslintignore
文件,它只包含这一行:
.eslintrc.js
You could simply comment that line too.您也可以简单地评论该行。
Hope that helps :)希望有帮助:)
In my case, this is because the eslint version installed is too old.就我而言,这是因为安装的 eslint 版本太旧了。 It seems that some old version just cann't format
javascript
.似乎某些旧版本无法格式化
javascript
。
For me, it seemed as if StandardJS was trying to lint everything in my node_modules
folder.对我来说,StandardJS 似乎正在尝试对我的
node_modules
文件夹中的所有内容进行 lint。 The error went away and linting started working again after adding a standard.ignore
property to my package.json
, followed by reloading VSCode:在将
standard.ignore
属性添加到我的package.json
后,错误消失了,linting 再次开始工作,然后重新加载 VSCode:
{
"name": "foo",
"devDependencies": {
"standard": "^16.0.4"
},
"dependencies": {
// stuff...
},
"standard": {
"ignore": [
"**/node_modules/**"
]
}
}
No idea why this behavior didn't happen automatically...不知道为什么这种行为没有自动发生......
UPDATE: I previously had installed eslint
alongside standard
, which may have been the root cause.更新:我之前在
standard
旁边安装了eslint
,这可能是根本原因。 My original install command was npm install standard eslint
, which installed the latest version of eslint
, v8.xx However, standard
(at the time of writing) depends on eslint
v7.xx, causing a conflict.我最初的安装命令是
npm install standard eslint
,它安装了最新版本的eslint
,v8.xx 但是, standard
(在撰写本文时)依赖于eslint
v7.xx,导致冲突。
The fix:修复:
~$ npm uninstall eslint # remove eslint from package.json
~$ rm -rf node_modules # remove node_modules
~$ npm install # get fresh installation
In my case this was even simpler than the other answers here: I forgot to create a .eslintrc.js
file for this project.就我而言,这比这里的其他答案更简单:我忘记为这个项目创建一个
.eslintrc.js
文件。 So running npx eslint --init
fixed it.所以运行
npx eslint --init
修复了它。 Copying the file from another project would also work.从另一个项目复制文件也可以。
Hope this helps someone like me googling this error and getting to this question.希望这可以帮助像我这样的人在谷歌上搜索这个错误并解决这个问题。
npm i eslint -g
, also install ESLint on the project using npm i eslint -D
npm i eslint -g
,同时使用npm i eslint -D
在项目上安装 ESLintsettings.json
does not have another default default formatter for JavaScriptsettings.json
没有 JavaScript 的另一个默认默认格式化程序{
"eslint.enable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnType": true,
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
}
}
That worked for me这对我有用
What worked for me was:对我有用的是:
rm -rf node_modules
npm cache clean --force
npm install
Close VScode and then re-open关闭 VScode 然后重新打开
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.