Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension 'eslint' is configured as formatter but it cannot format 'JavaScript'-files

I am experiencing an intermittent problem with ESLint in VS Code. When saving a file, instead of formatting it, this message shows in the status bar:

enter image description here

Apparently no one has ever reported this message on Google.

When I say it is intermittent, it was working fine, then the computer (MBP) crashed, and on restarting this is the situation. 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. In 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:

        "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.

EDIT

This issue regarding TypeScript gives some hints. It seems this error can result when the ESLint server is restarting or the extension host is under a lot of stress.

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. 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.

like image 644
Steve Bennett Avatar asked Oct 28 '21 04:10

Steve Bennett


People also ask

Is ESLint a formatter?

ESLint comes with several built-in formatters to control the appearance of the linting results, and supports third-party formatters as well. You can specify a formatter using the --format or -f flag on the command line. For example, --format json uses the json formatter.

How do I fix ESLint errors in Visual Studio code?

In your code editor, open the JavaScript file you created earlier. All the broken rules will be indicated. If you have the ESLint extension installed you can use CTRL+SHIFT+P to open the Command Palette. Then search for ESLint: Fix all auto-fixable Problems and press ENTER (or RETURN ).

What is ESLint extension in VS Code?

ESLint supports you and teams to follow a common code style in your project. It can be used in VS Code by installing it from the VS Code Marketplace. Once you have integrated it in VS Code, you can configure ESLint to enforce a code style in your files.

Why is ESLint not formatting my typescript files?

This issue regarding TypeScript gives some hints. It seems this error can result when the ESLint server is restarting or the extension host is under a lot of stress. In my project now, most files are formatting fine, although one consistently fails.

How to Lint and format code with ESLint in Visual Studio Code?

How To Lint and Format Code with ESLint in Visual Studio Code Step 1 — Creating JavaScript Starter Code. You need to start with a demo project. ... Open app.js in Visual Studio Code. Step 2 — Setting Up ESLint. It’s important to include the --save-dev flag because this saves the package as a ...

Why can't I format selections in ESLint?

The ESLint extension has no support for formatting selections since ESLint itself (the npm module) has not API for it. This is the reason why you can't do it.

What configuration should I use for ESLint in JavaScript?

JavaScript (ESM) - use .eslintrc.cjs when running ESLint in JavaScript packages that specify "type":"module" in their package.json. Note that ESLint does not support ESM configuration at this time. YAML - use .eslintrc.yaml or .eslintrc.yml to define the configuration structure. JSON - use .eslintrc.json to define the configuration structure.


3 Answers

I was actually experiencing the same problem and I only needed to remove the .eslintignore file, which only contained this line:

.eslintrc.js

You could simply comment that line too.

Hope that helps :)

like image 167
Rodrigo D'Agostino Avatar answered Oct 11 '22 23:10

Rodrigo D'Agostino


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:

Settings Screenshot

like image 9
Tugberk Sengezer Avatar answered Oct 11 '22 23:10

Tugberk Sengezer


This can happen for a variety of reasons. The eslint output logs will tell you the issue.

enter image description here

In my case the settings field configFile was changed to overrideConfigFile

like image 7
Peter Hayman Avatar answered Oct 11 '22 23:10

Peter Hayman