37

How to turn off the prettier trailing comma in VS Code?

I go to settings. Select none for the prettier trailing comma. Restart VS Code. Select a piece of TypeScript, press Ctrl + Shift + F and the trailing commas still appear. How could I fix it?

enter image description here

manymanymore
  • 2,251
  • 3
  • 26
  • 48
  • 2
    Maybe you have a `.prettierrc` config file in your project that overrides the VSCode settings ? – Ewaren May 18 '20 at 18:56
  • @Ewaren, by searching in a project (`Ctrl` + `P` and typing the `.prettierrc`) I can not find such a file. But by searching through all the files (`Ctrl` + `Shift` + `F`) I can find a file `settings.json` and in the file I has only this: `{"prettier.trailingComma": "none"}`. – manymanymore May 18 '20 at 19:03
  • the `settings.json` file contains your workspace settings : it is just the JSON transcription of the settings you chose in VSCode's graphical interface. There must be a config file in your project overriding these settings, it would be easier to help you if you could show us the folder structure of your project (for example with the `tree -a` command). – Ewaren May 18 '20 at 19:15
  • @Ewaren, I am not sure where I should execute the command. My folder structure is an ordinary Angular app structure. I got it after running the `ng new angular-tour-of-heroes`. When I try to run the command in my project folder I am getting the error: `$ tree -a bash: tree: command not found` – manymanymore May 18 '20 at 19:18
  • I just looked up the source code for Tour of Heroes, there is no .prettierrc file there, maybe the .editorconfig file introduces conflicts... Try adding a .prettierrc file at the root of your project, with `{ "trailingComma": "none" }` in it maybe. – Ewaren May 18 '20 at 21:50
  • @Ewaren, just have checked. That helped. Thank you a lot. Could you post your last comment as an answer, please? :) – manymanymore May 19 '20 at 20:17

10 Answers10

54

Since you are working on the Tour of Heroes project, it is maybe the .editorconfig file there that introduces conflicts with your VSCode Prettier settings. Try adding the following .prettierrc file at the root of your project :

{
  "trailingComma": "none"
}

The .prettierrc file has the highest priority over any setting, so it should override any conflict.

Ewaren
  • 1,343
  • 1
  • 10
  • 18
14

Adding this line to settings.json worked for me.

"prettier.trailingComma": "none"

Er. Sachish
  • 369
  • 2
  • 13
11

At the root of the project create the Prettier configuration file: .prettierrc.json

Add this code to your file: .prettierrc.json

{
    "trailingComma": "none"
}

Save file and then restart your Visual Studio Code

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

I was facing same problem and I added this line in the settings and it worked for me.

"prettier.trailingComma": "none"
Andrew
  • 139
  • 2
  • 3
4

In VS Code Settings, go to json file and type this:

"prettier.useEditorConfig": false

Or just in the Setting, under Prettier configs, set this to false/uncheck to prevent prettier other config to take precedence over Prettier

Sterling Diaz
  • 3,789
  • 2
  • 31
  • 35
  • 1
    This is the solution to all the prettier related problems. I think we shouldn't be touching `.editorconfig` unless we are doing for the whole team – newbie Jul 05 '22 at 09:39
3

Prettier has the following rules for where to look for settings (in order of precedence):

  • A "prettier" key in your package.json file.
  • A .prettierrc file written in JSON or YAML.
  • A .prettierrc.json, .prettierrc.yml, .prettierrc.yaml, or .prettierrc.json5 file.
  • A .prettierrc.js, .prettierrc.cjs, prettier.config.js, or prettier.config.cjs file that exports an object using module.exports.
  • A .prettierrc.toml file.

Following the first rule, I was able to configure it by adding the following to package.json

"prettier": {
  "trailingComma": "none"
}
John
  • 10,165
  • 5
  • 55
  • 71
2

I had the same experience as your screen recording. Restarting VSCode did the trick for me. I could not find a way to restart the prettier addon... maybe someone can chime in on how to do that.

Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
1

Run this command:

npm run lint --fix

after formatting...

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
  • this doesn't change anything in the vscode settings. – Stealth Rabbi Feb 22 '22 at 16:52
  • @banushan-puviraj what do u refer by running `npm run lint --fix` you should have shared the `lint` script as well, not sure but u may have configured `lint` to 'eslint .` from scripts in packgage.json file. Thanks – Wasit Shafi Mar 22 '23 at 15:19
1

In my case, the configuration above was not enough.

For this to work for me, in addition to this setting, I had to remove from setting.json:

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

Next, enter the command ctrl + shift + P in vscode

Click on Configure...

Choose Prettier - Code formatter

Now this:

const a = {
  a: 10,
  b: 15,
};

turns into this:

const a = {
  a: 10,
  b: 15
}
Roma N
  • 175
  • 11
0

Try this in vs-code settings.json

// remove semicolons
"prettier.semi": false
Emma
  • 658
  • 10
  • 20