Prettier doesn't format .tsx file

25,336

Solution 1

Edit setting with following in settings.json of VScode

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

Solution 2

Expanding on iNeelPatel's answer, I had to add two lines to VSCode JSON settings:

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

Solution 3

This will give perfect solution

"Same thing happened to me just now. I set prettier as the Default Formatter in Settings and it started working again. My Default Formatter was null."

https://github.com/microsoft/vscode/issues/108447#issuecomment-706642248

Solution 4

An easy UI alternative to what has been proposed already:

  1. Search for "default formatter" in your vscode settings.
  2. Click on "Text Editor" and set the default formatter to "Prettier - Code formatter".
  3. Enjoy.

Solution 5

Create a .vscode folder at the root of your project. In the .vscode folder, create the settings.json file, and in it, write this section:

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

Don't forget to add .vscode in the .gitignore file.

Share:
25,336

Related videos on Youtube

S. Hesam
Author by

S. Hesam

I love learning. I used to study physics and I am very interested in physics. I was using the C programming language to simulate the interaction of lasers and electrons, and I wrote various programs for my research. When I was programming for the numerical resolution of physics issues, I realized that I deeply loved programming, So I decided to become a programmer.

Updated on July 09, 2022

Comments

  • S. Hesam
    S. Hesam almost 2 years

    I've used the Prettier extension in the visual studio code editor for a long time, but recently I am writing to React with Typescript. So I need to configure for Prettier to format .tsx files.

    • Emile Bergeron
      Emile Bergeron almost 4 years
      Do you have prettier as a dependency of this project?
  • smbergin79
    smbergin79 almost 3 years
    You just made my day!
  • Dylan Kapp
    Dylan Kapp over 2 years
    In case you are wondering how to access settings.json -> stackoverflow.com/a/65909052/5888998
  • Uche Azinge
    Uche Azinge over 2 years
    To open settings follow these steps: Shift + CMD + P, Type "settings", Click on "preferences: open settings(JSON)"
  • chichi
    chichi over 2 years
    THANK YOU!!!!!!
  • VINIT CHURI
    VINIT CHURI about 2 years
    worked for me 👍