657

In my Nuxt application where ESlint and Prettier are installed and enabled, I switched to Visual Studio Code.

When I open a .vue file and press CMD+ Shift + P and choose Format Document, my file does not get formatted at all.

My .prettierrc settings:

{
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true
}

I have so many source code lines, so I cannot format them manually. What am I doing wrong?

13
  • 6
    Seems that prettier by default has prettier.disableLanguages set to ["vue"] Commented Oct 1, 2018 at 8:25
  • 2
    There's also issue for v1.27.2 with note that the same config worked for v.1.23 Commented Oct 1, 2018 at 9:41
  • 77
    Upvoted b/c just pressing CMD+ Shift + P then Format Document, then choosing Prettier as my default formatter helped me :D Commented Oct 25, 2019 at 9:59
  • 2
    For me, it was not working even after making all the configuration setting changes. Then, I realized I was trying to format a code fragment var x = "test"; written in HTML file when I should have written it in a *.JS file. When I wrote the JS code into a JS file it got formatted to var x = 'test'. Commented Apr 8, 2021 at 4:34
  • 5
    Try to disable prettier –> reload vscode -> enable prettier Commented Sep 19, 2021 at 9:28

54 Answers 54

1
2
3

Some times with auto plugins update Required files used by Prettier might go missing .

Check this path if files are present here or folder is empty

C:\Users\YOURUSERNAME\.vscode\extensions\esbenp.prettier-vscode-2.2.2\out

If missing uninstall and reinstall prettier

Sign up to request clarification or add additional context in comments.

1 Comment

> "If missing uninstall and reinstall prettier" is what did it for me. I had all the settings right, and running prettier from an NPM script even worked right, but formatting on save didn't until I uninstalled and reinstalled.
3

I Rolling back prettier to 1.7.3 and fixed it

Comments

3

For me - it was to do with ESlint which also works with Prettier. Eslint wasn't working (a local installation vs global installation conflict) which broke Prettier.

2 Comments

How to resolved this. I am having an issue where Eslint is asking for a new line when press enter and save it gets back to the same position and error starts appearing again.
If you do have an ESlint config already, you should probably use Prettier via ESlint. Here is my answer on how to achieve such thing.
3

The only thing I needed to do was update my packages with a simple

npm install

I tried everything on here and it was still not working. Just seemed like it does not exist. Prettier was also added as a dependency in the project. It turned out that someone else had added or updated any package which caused my prettier to stop working.

Hoping it saves someone the headache it caused me.

2 Comments

npm install installs the dependecies specified in your package.json + their version ranges. It doesn't update anything per-se. Important difference IMO.
Yes @kissu you are right. The important things here is that because of the difference prettier stopped working. And after running it, prettier was able to work again. So may be for someone else who is also overlooking this it will be helpful.
3

I am working in WSL (will also work for people having the problem only in cutom workspaces) and I tried EVERY fix possible until I found this tab on vscode settings: Image

Select the tab regarding your problem and change all the settings you want again and change the setting "Files: Auto Save" to either "onFocusChange" or "onWindowChange".

1 Comment

This was it for me too – didn't realize I had to update Workspace as well as user settings.
2

In my case I had to do the following:

  1. Install prettier from the command line(npm install --save-dev --save-exact prettier prettier-plugin-custom)
  2. Reload VSCode

and voilà, everything started working.

TIP: To make sure the installation is good I checked for the version:

npx prettier --version

1 Comment

This worked for me. I had prettier installed globally already, but it didn't work. If I had to guess, it might have been a WSL issue.
2

Check for requirePragma in .prettierrc, it says you need to add a top level comment for that file to be formatted

remove that rule and it should work

Comments

2

For me helped disabled this option in VS Code settings (then prettier use default config):

Prettier: Require Config

Comments

2

Default formatter with modified elsewhere tag

For those looking to get this working with Prisma and other overriding extensions or project settings (like in the image above), use the following:

// Inside .vscode/settings.json
{
  // other extensions
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[prisma]": { // replace this with the language that is overriding 
    "editor.defaultFormatter": "Prisma.prisma" // Replace this with the extension/formatter you want for that language
  },
  // rest of .vscode/settings.json
}

You shouldn't override project settings by forcing your editor settings as some other answers would suggest. This will just annoy your team and or whoever manages the repository you're working on.

Comments

1

There are a few syntaxes that prettier is not able to parse. ?? is one of them. After removing the symbol my prettier worked like a charm. You should take a look at the output of the Prettier extension in vscode. That should point out the line and the syntax which is causing problems for prettier to format properly.

Comments

1

I tried to enable "Format On Save" and set the "Auto Save Delay" to 0. It worked, so I guess you could try this too.

Edit: You can see them through these steps.

  1. Manage(The gear Icon bottom left)
  1. Settings(Inside the manage dropdown list)
  1. Search for Format On Save and enable it(check the box)
  1. Scroll down and look for Auto Save Delay and set it to 0

Comments

1

If you have set all things which you can do in Setting and Prettier is not working. Try to install it through this command line. npm install --save-dev prettier

1 Comment

Despite npx prettier --version working, I wasn't getting any auto-formatting until I ran the install command in this answer. (I used yarn, but the result is the same.)
1

here my prettier config working on vue.js files, typescript files and json files.

arrowParens: 'always'
bracketSpacing: true
endOfLine: 'crlf'
htmlWhitespaceSensitivity: 'css'
insertPragma: false
jsxBracketSameLine: false
jsxSingleQuote: true
overrides:
- files: '*.json'
  options:
    semi: true
    parser: 'json'
parser: 'babel'
printWidth: 120
proseWrap: 'preserve'
quoteProps: 'truepreserve'
requirePragma: false
semi: false
singleQuote: true
tabWidth: 8
trailingComma: 'es5'
useTabs: true
vueIndentScriptAndStyle: 

Do not forget to update your vscode settings

{
"extensions.ignoreRecommendations": false,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[vue]": {
    "editor.defaultFormatter": "octref.vetur"
},
"[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
},
"vetur.format.options.useTabs": true}

that's all folk's !

1 Comment

"[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, Worked perfectly for me! Otherwise everything else was set fine but still prettier was not working on save.
1

For my case i was using a windows machine and it turned out the file system had been corrupted thus prettier raised an error because it could not be able to open the config file due to file system corruption.

TO resolve it I run chkdsk /f h: on the terminal where h is the file partition.

This resolved the file corruption and prettier is now working fine.

Comments

1

I have same problem when I moved from Ubuntu to Windows 10.

The problem was with shortcuts 'ctrl + shirt + i', which was default on Ubuntu but was not in Win10.

Go to file -> preferences -> keyboard shortcuts.

Then write prettier and find 'Format Document (Forced)'. Click on pencil on left and write keyboard shortcut, press Enter.

If it is not possible because there is already command written to that shortcut, find it and change it to something else. Then it will be possible.

enter image description here

Comments

1

Consider reading Prettier's log output from here

enter image description here

For me - and since the log output says that he couldn't find .prettierrc.json file - renaming .prettierrc file to .prettierrc.json solved that for me!

Comments

0

Don't forget to enable "editor.defaultFormatter" in the settings of the VSCode. In my case it was null and hence even the "editor.formatOnSave" was also not seeming to solve the issue.

1 Comment

What does this add to the other 4 answers that mention the same thing?
0

For me it worked when I got rid of parser: json in the .pretterrc file.

Comments

0

In the unlikely event that none of the above solutions work for you (like me), I had to set the default formatter to null in settings, then hit CTRL + SHIFT + P and search for "format document" (the default format document binding +R didn't work for me) then I was prompted to select a default formatter in a popup and after selecting prettier it formatted the document and also formats on saving now

1 Comment

Not sure why someone downvoted this. I tried all the solutions being extolled, none worked. Then I set my formatter to Null, closed/reopened VS Code, formatted a document and Prettier magically started working since it detected my .prettierrc file. Previously it kept trying to use the VS Code settings even though I told it to user Prettier.
0

Just in case, since its not already here...

I was trying to format a minified .js file in my node_modules folder so I could understand the code better and Prettier kept returning:

["INFO" - 7:13:19 PM] File is ignored, skipping.

In this case, you can force-format by opening the Command Palette Ctrl+Shift+P and then selecting Format Document (Forced) to have it format a file that would normally be ignored.

Forced

Comments

0

I've tried done all stuff but my codebase is not getting formatted. Then I checked my .prettierignore & some other developer just wrote /src there.

I removed it & everything is working fine now

Comments

0

The answer is scattered in all of these answers so I'll consolidate (because it could be the same as my problem, more than 1 setting not configured correctly)

  1. Make sure that your default formatter is set.
    default formatter

  2. Set your configuration file.
    config file

  3. Set Prettier Path.
    prettier path

This should fix it, but make sure that your .prettierignore file is also set (just a little bit above the path option).
prettierignore path

If you're using prettier globally vs locally, make sure the prettier path is set to your global install location (you can figure this out by typing $which prettier providing that you're using Linux.

Comments

0

I had no error in the ouput tab, but using the command Format Document With... actually throws an error, so I knew something was wrong.

In the end, the issue was that in my monorepo I had an empty node_modules folder at the root which made Prettier search here instead of the children folders. Deleting it fixed the issue.

Comments

-4

I tried every single solution in this thread, and I just realized that my only issue was my Visual Studio color theme. That simple!

Before: enter image description here

After: enter image description here

1 Comment

The emojis tho!
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.