Udhav Sarvaiya
Udhav Sarvaiya

Reputation: 10071

"Prettier - Code formatter" is not working in Visual Studio code

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

Upvotes: 27

Views: 96730

Answers (12)

Anant Rawat
Anant Rawat

Reputation: 169

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

Upvotes: 16

WhiteThundra
WhiteThundra

Reputation: 59

Maybe try deleting the whole node_modules folder and running npm install again (when using node)

Upvotes: -1

Kien Nguyen Ngoc
Kien Nguyen Ngoc

Reputation: 432

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

Upvotes: 1

Ferhi Malek
Ferhi Malek

Reputation: 514

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

Upvotes: 0

Filip
Filip

Reputation: 1

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

Upvotes: -1

Manan Gadhiya
Manan Gadhiya

Reputation: 550

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.

Upvotes: 32

Nick Manning
Nick Manning

Reputation: 2989

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.

Upvotes: 2

Zidong
Zidong

Reputation: 119

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

Upvotes: 0

Félix Paradis
Félix Paradis

Reputation: 6031

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

Upvotes: 2

rafaelje
rafaelje

Reputation: 29

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',
};

Upvotes: 1

selharem
selharem

Reputation: 74

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

Upvotes: 2

Konrad Grzyb
Konrad Grzyb

Reputation: 1985

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.

Upvotes: 5

Related Questions