97

For about a month now, my Prettier extension has stopped working as it normally does. Most notably - format on save does not work. Other notes:

  • Format on typing a ; works
  • I get this error type symbol in the status bar next to the Prettier button - I can't find any reference to this relating to Prettier on Google:

    enter image description here]

  • I have my format on save checked in Settings:

    Format on save

  • 'Format Document,Format SelectionandFormat Document with` command palette options all do nothing

  • Timing of the break coincides with moving from Prettier 2-3
  • I've followed the migration steps and think all is in order. Some snippets of the configuration between eslint and prettier that I have:

    1. As per the prettier documentation, my VS Code settings has:
"eslint.autoFixOnSave": true, // (even though VSCode has this as deprecated), have tried with and without this line
"editor.codeActionsOnSave": {
  "source.organizeImports": true,
  "source.fixAll.eslint": true
},
  1. My Dev dependencies include:
    "eslint-config-prettier": "^6.5.0",
    "eslint-plugin-prettier": "^3.1.2",
    "prettier-eslint": "^9.0.0",
  1. I have a prettier.config.js file
  2. My .eslintrc.js file includes:
extends: ['airbnb', 'prettier'],
plugins: ['react', 'jsx-a11y', 'import', 'react-hooks', 'plugin:prettier/recommended'],

Anyone have any idea on how to fix this or further debugging tests to do here? It's driving me nuts!

Gama11
  • 31,714
  • 9
  • 78
  • 100
Hemal
  • 1,617
  • 2
  • 12
  • 21
  • SUGGESTIONS: 1) Check out the readme [here](https://github.com/prettier/prettier-vscode). Especially the part about "breaking changes between 2.x and 3.x). Uninstall and reinstall. 3) Ensure you install the latest/greatest version. 4) Post back what you find. – FoggyDay Dec 21 '19 at 03:43
  • Have uninstalled and reinstalled the latest version. The migration from 2-3 timing seems about right in terms of when things broke for me, so it seems likely that this is related to that. I've updated my qn with the relevant migration steps taken. Thanks for trying to help! – Hemal Dec 21 '19 at 04:37
  • It sounds like others are experiencing the same problem: https://github.com/prettier/prettier-vscode/issues/1156 – FoggyDay Dec 21 '19 at 05:39

15 Answers15

306

Follow these steps:

  1. CTRL + SHIFT + P
  2. Format Document (in pop-up bar)
  3. Select Format Document
  4. Select Configure Default Formatter...
  5. Select Prettier - Code formatter

Done!

TasDiam
  • 677
  • 8
  • 11
Goku
  • 3,329
  • 1
  • 8
  • 9
  • 8
    I noticed that prettier was not working after a VS code update. I don't know if it has to do with the problem but if it is, then the problem may re-appear on future updates, and this solution may work again on those cases. – TasDiam Oct 15 '20 at 15:18
  • Cheers mate. This seemed to resolve itself (I tried a million things and not really sure which one of them worked). I do understand it a bit better and can see the logic behind your process. For me, the configuration was already set to Prettier but it still wasn't working (you can see that Prettier has been configured, I think by virtue of the Prettier button on the bottom right of the first screenshot. – Hemal Oct 16 '20 at 01:12
  • Also! If there's any syntax error, or a comment is unclosed somewhere in script, format on save or Prettier won't work. Best of luck and happy hacking :) – Goku Oct 18 '20 at 00:11
  • Still helpful for VSCode Version: 1.55.0! – FullStackOfPancakes Apr 08 '21 at 16:45
  • 6
    This didn't work for me unfortunately - already had Prettier set as default formatter and also format on save turned on. – tikej Jul 29 '21 at 08:53
  • didn't work for me!!!! set Prettier as default, format on save, restart VS Code. And still got a message saying Prettier cannot format my .js – Tony Lucas Aug 24 '21 at 13:40
  • Configure default formatter didn't show up on mine so I had to change the json and put in... "editor.defaultFormatter": "esbenp.prettier-vscode". In case anyone runs into this – Kalimantan Jan 29 '22 at 02:31
  • When I select Format Document, it says "There is no formatter for 'apex' files installed. – VISHNU SANTHOSH Jan 21 '23 at 05:16
  • 2
    Solved it for me! VS Code had 2 formatters for HTML. – Ken Palmer Feb 04 '23 at 02:47
  • doesn`r work for me – dzhukov Aug 23 '23 at 14:18
75

In VSCode settings, search for "Editor: Default Formatter" and set it to esbenp.prettier-vscode

Hamed
  • 761
  • 5
  • 3
26

On VScode go to Settings > Text Editor > Formatting

Then check the Format On Save checkbox.

enter image description here

Julie Rankata
  • 774
  • 8
  • 12
21

Configuration has changed you need to add this into you vs-code settings:

According to the documentation: "You can enable Auto-Fix on Save for ESLint, TSLint or Stylelint and still have formatting and quick fixes"

"editor.codeActionsOnSave": {
  // For ESLint
  "source.fixAll.eslint": true,
  // For TSLint
  "source.fixAll.tslint": true,
  // For Stylelint
  "source.fixAll.stylelint": true
}
  • 3
    Or `"source.fixAll": true` [to fix all generic linting](https://code.visualstudio.com/updates/v1_41). I use 1.56.2 version of VSCode. – Seno Jun 07 '21 at 10:54
17

For me, using prettier+(svipas.prettier-plus) -- because the default prettier plugin is no good -- ONLY changing:

"editor.formatOnSaveMode": "modifications",

to

"editor.formatOnSaveMode": "file",

solved my problem.

SgtPooki
  • 11,012
  • 5
  • 37
  • 46
15

in my case the issue was that the default formatter setting was not being used for typescript files. Looking at my settings.json I saw

    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "[typescript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features"
    },
    "[typescriptreact]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },

After adjusting the value for [typescript] the issue was resolved.
I couldn't find a place Settings UI where the default formatter can be changed for TS only, but it would be a nice feature though.

rads
  • 191
  • 1
  • 4
12

Try to make your code prettier manually by pressing CTRL + SHIFT + P >>> Format Document. If your file is being formatted without any issues, it means that the issue lies in formatOnSave settings. Probably, you can try to make further debugging from there.

Alexander Borisov
  • 1,087
  • 12
  • 13
  • 3
    This was the solution that worked for me. When I followed these instructions, VS Code asked me which formatter I wanted to use to format the document even though prettier was already set in the settings.json file. I would add that if this does not work, then try running the prettier command from the command line to see if the binary is even installed, etc. – Ted Stresen-Reuter Apr 05 '21 at 16:23
  • This worked for me. Same approach as Hamed's answer above, but a little better in my opinion since CTL + SHIFT + P gives you the option of selecting the value that Hamed suggests you type in manually (esbenp.prettier-vscode). Not sure what that value signifies, but I'm more comfortable selecting it from an editor option than typing it manually. – Woodchuck Nov 21 '22 at 23:18
3

For formatonSave was already checked and the defaultFormatter was prettier ... when I switched into settings.json, it was like!!

"[javascript]": { "editor.formatOnSave": false }, "eslint.autoFixOnSave": true, "prettier.disableLanguages": [ "js" ],

After changing it to

"[javascript]": { "editor.formatOnSave": true }, "eslint.autoFixOnSave": true, "prettier.disableLanguages": [],

it did work...

Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42
1

First check if Prettier works fine. You can try to format a single file using Shift + Cmd + P and Format Document. If this doesn't work then it is most likely an issue with Prettier extension. Disable and Enable Prettier extension from Extensions.

If Formant Document works fine on single file then the best way to resolve these type of issues is going to settings.json and looking up the config. I had an issue where the first few lines in settings.json had issues and hence the other settings like format on save (editor.formatOnSave) and defaultFormatter which were below the ill-formatted line wasn't working.

Shetty87
  • 33
  • 6
1

I tried checking the "format on save" box but it didn't do it for me, even prettier selected as a default formatter. Editing the settings' json file worked for me :

  1. Open the command palette with ctrl + maj + p

  2. Search 'open settings json'

  3. Select the user option enter image description here

  4. In the file look for "editor.defaultFormatter" for html (line 16 in my case)

  5. Set its value to "esbenp.prettier-vscode" enter image description here

Done

Just Born
  • 11
  • 3
0

I had to add a jsconfig.json file containing:

{
  "exclude": ["node_modules"]
}

to my app directory, and reload Visual Code, in order for Prettier to work. Also, I can't choose "Format Document With..." in the Visual Code command palette until I add this file.

Basically it tells Visual Code that the project is written in JavaScript (if I understand it correctly) and to exclude node_modules from intellisense.

This was after doing everything else people mentioned here.

ouflak
  • 2,458
  • 10
  • 44
  • 49
post4k
  • 1
  • 1
0

I had one repo formatting on save and another not, in two different VSCode windows. The broken repo was using this filename:

// incorrect
.prettierrc.json

The functioning repo was using this filename:

// correct
.prettierrc

Dropping the .json extension corrected the problem.

Joe Lapp
  • 2,435
  • 3
  • 30
  • 42
0

I dont use prettier but What worked for me was , syncing my user with vs code.

What should be "on"

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '23 at 03:21
0

Here is my solution when using black to format Python scripts.

Ctrl+Shift+P and select Open Settings (UI), type Python Formatting in the search bar, and then:

  • Python > Formatting: Black Path: Specify the path where black executable file is located.
  • Python > Formatting: Provider: Modify none to black.
    • This change makes my editor to work. Somehow this setting would be reset by some extensions or careless operations.
Hansimov
  • 597
  • 7
  • 10
0

If all of the above doesn't work for you, check your settings.json file and confirm the defaultFormatter set for Javascript.

"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.fixAll": true
    }
  },

In my own case, it was set "vscode.typescript-language-features" then I changed it to esbenp.prettier-vscode and it worked!

Fatimah
  • 722
  • 7
  • 16