22

I have Visual Code Studio(1.41.1) Editor and I need Auto Formatter

I have installed this code format: Prettier - Code formatter Successfully installed but not working,

I also tried to use the command ext install esbenp.prettier-vscode that too was successful installation but is not working.

I checked that the composer is installed properly and the environment variable path is given correctly in my system, Also, I added it to the settings.json file by looking in the document: "phpformatter.composer": true but not success in auto-formatting my code

Visual Studio Code and System Restart also tried but did not succeed

Why can't I Auto Formatter in my Visual Studio code use this "Prettier - Code Formatter"? No errors are received, but the auto formatter is not working

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64

11 Answers11

27

try this, it worked for me

File -> Preferences -> Settings (for Windows)

Code -> Preferences -> Settings (for Mac)

Search for "Default Formatter". In the dropdown, prettier will show as esbenp.prettier-vscode.

Bhavya Koshiya
  • 1,262
  • 2
  • 8
  • 22
Manan Gadhiya
  • 480
  • 4
  • 6
14

Maybe the prettier extension is not working for javascript file. Open your settings.json for vs code and paste

{ "editor.defaultFormatter": "esbenp.prettier-vscode", "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } }

This might work for you

n0nvme
  • 1,248
  • 8
  • 18
Anant Rawat
  • 141
  • 1
  • 3
6

You may have local .vscode directory in the root of your project with settings that prevent formatting. Try to edit or remove .vscode local configurations if you have. After close and reopen VS Code.

Konrad Grzyb
  • 1,561
  • 16
  • 12
  • This was my problem. A repo I added had it's own prettier setup. I need to run "npm install" so that it added the correct local prettier setup. – Sean Rasmussen May 13 '21 at 13:47
  • After spending a week in almost my 2 different Mac I came along this and this resolved the issue.just deleted that and every setting is now applied from global settings.json file – Ravi Yadav Aug 07 '21 at 14:38
2

as @harkesh kumar said : press Ctrl+shift+p and type format document then select prettier format, it may work for you

selharem
  • 74
  • 12
2

For me it was failing silently because I was using await inside of a function that wasn't marked with async. That raises another question - why would it fail silently here. Regardless, once I fixed that error then prettier began working again.

Nick Manning
  • 2,828
  • 1
  • 29
  • 50
1

Make sure you have a file in the root folder with this name .prettierrc.js

inside you should have something like:

module.exports = {
  bracketSpacing: false,
  jsxBracketSameLine: true,
  singleQuote: true,
  trailingComma: 'all',
  arrowParens: 'avoid',
};
rafaelje
  • 29
  • 4
1

If you don't have and don't want a prettier config file for some reason, you can uncheck the option "Prettier: Require Config"

In settings.json it's "prettier.requireConfig": true

In the UI it looks like this: Screenshot of the option in the settings

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49
0

I finally found the issue. I was trying to format code on a remote server.

In the extension page for Prettier, it says:

This extension is disabled in this workspace because it is defined to run in the Remote Extension Host

Then I clicked on the button "install on remote host ..." and it worked

Zidong
  • 121
  • 1
  • 3
  • 7
0

If you wish you can add this formatter instead of prettier for javascript. Open settings.json and enter this for js formatter: "vscode.typescript-language-features"

steps here

Filip
  • 1
  • 2
0

My answer is for vs code formatting in C#: and I had this same issue, thanks to @selharem's answer above, I followed the same steps, but encountered " Prettier is the default formatter, but I could not format c# code", upon pressing configure, I had three c# formatter option to select from, one of them was "Csharpier" the c# code formatted in vs code. There is this, and the second option was the command "dotnet Format", which is included in the official .Net SDK since .Net6

Ferhi Malek
  • 484
  • 4
  • 15
0

disable Prettier -> reload your VSCode -> enable Prettier again. Sometime it cache and not apply your setting.