简体   繁体   English

扩展“eslint”被配置为格式化程序,但它无法格式化“JavaScript”文件

[英]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 fighting with this for a while, turns out you need to head to Settings , under User go to Extensions > ESLint , scroll down to Format:Enable and enable ESLint as a formatter.我为此奋斗了一段时间,结果你需要前往Settings ,在User下转到Extensions > ESLint ,向下滚动到Format:Enable并启用ESLint作为格式化程序。

Settings:设置:

设置截图

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 :)希望有帮助:)

This can happen for a variety of reasons.这可能由于多种原因而发生。 The eslint output logs will tell you the issue. eslint 输出日志会告诉你问题。

在此处输入图像描述

In my case the settings field configFile was changed to overrideConfigFile在我的情况下,设置字段configFile已更改为overrideConfigFile

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.希望这可以帮助像我这样的人在谷歌上搜索这个错误并解决这个问题。

  1. Disable ESLint extension禁用 ESLint 扩展
  2. Reload window重新加载 window
  3. Enable ESLint extension启用 ESLint 扩展
  4. Do npm i eslint -g , also install ESLint on the project using npm i eslint -D执行npm i eslint -g ,同时使用npm i eslint -D在项目上安装 ESLint
  5. Make sure VSCode settings.json does not have another default default formatter for JavaScript确保 VSCode settings.json没有 JavaScript 的另一个默认默认格式化程序
{
  "eslint.enable": true,
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "dbaeumer.vscode-eslint",
  "editor.formatOnType": true,
  "[javascript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint",
  }
}
  1. Reload window重新加载 window

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.

相关问题 在 Chrome 扩展程序中看不到任何 JavaScript 文件 - Cannot see any JavaScript files in a Chrome extension 如何使用eslint格式化我的javascript代码? - How to use eslint to format my javascript code? Javascript Eslint 错误:找不到模块“optionator” - Javascript Eslint Error: Cannot find module 'optionator' 对 ESLINT 和 Stylelint 等配置文件使用 JS 扩展有什么好处吗? - Is there any benefit to use JS extension for config files like ESLINT and Stylelint? 我们如何考虑将工作空间文件考虑为eslint扩展? - How can we put workspace files in consideration for eslint extension? 带有 eslint-config-airbnb 的扩展名为“.js”的文件中不允许使用 JSX - JSX not allowed in files with extension ' .js' with eslint-config-airbnb javascript格式化程序 - javascript formatter 如何通过VSCode上的ESLint在JavaScript中格式化function周围的空间? - How to format space around function in JavaScript by ESLint on VSCode? 用户名不能为电子邮件格式,因为已为电子邮件配置了用户池 - Username cannot be of email format, since user pool is configured for email 混合 Typescript 和 Javascript Repo - eslint - 影响 JS 文件的 TS 规则 - Mixed Typescript and Javascript Repo - eslint - TS rules affecting JS files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM