0

I am using VS Code and would like to exclude a specific json file data.json from being formatted by Prettier on save. Say it's in the root, then I create a file .prettierignore and add a line data.json (according to the docs).

This works fine with other types of files, but not with json files. In fact, even writing *.json will still format the json files.

There is a setting in VS Code

JSON > Format:Enable
Enable/disabled default JSON formatter

which is enabled. When I disabled it, however, no json file will be formatted on save. This is not what I want. I only want to exclude a specific json file. How can I achieve this?

I have already seen the related question 46409892.

kaya3
  • 47,440
  • 4
  • 68
  • 97
Script Raccoon
  • 261
  • 2
  • 14

1 Answers1

1

I think your VS Code formatter is not Prettier by default for JSON (or maybe not your default formatter for all documents ?)

To define a formatter by default, according to Prettier doc, you have to set your VS Code settings file like this :

{
  // For all files
  "editor.defaultFormatter": "esbenp.prettier-vscode",

  // For JSON specifically
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

Then, you can define ignored files in your .prettierignore using same syntax as .gitignore.

maximalain
  • 26
  • 4
  • Perfect! This solved the issue. Sorry for the late reply. In my settings.json Prettier was already configured as the default formatter, but for json it had: `"[json]": {"editor.defaultFormatter": "vscode.json-language-features"},` for some reason. I changed it to Prettier and now it works. :) – Script Raccoon Mar 14 '23 at 18:48
  • Or simply removing that line also works, since Prettier is already the default. – Script Raccoon Mar 14 '23 at 18:52