Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Prettier does nothing when you format a .tsx file in VS Code

ErrorFormat Document does nothing on a .tsx file

Short answer

When Prettier ignores .tsx files, VS Code is usually using another formatter for that language, the file is excluded, or a setting blocks it. Set esbenp.prettier-vscode as the default formatter for typescriptreact in its own block, then check .prettierignore, prettier.requireConfig and format on save.

  • Applies to VS Code, Prettier extension esbenp.prettier-vscode, Prettier 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the Prettier output from the status bar while the .tsx file is active. Check it for error messages.

    // Click "Prettier" in the status bar (bottom right) with the .tsx file open
  2. 2Match what you see, then jump to its fix

    1. 1Format Document asks which formatter to use, or another formatter runsPrettier is not the default formatter for TypeScript React filesFix
    2. 2Manual Format Document works, saving does not formatFormat on save is not on for that languageFix
    3. 3The file matches a pattern in .prettierignore, or sits in node_modulesThe file is ignored by .prettierignoreFix
    4. 4Formatting works in projects that have a .prettierrc and stays silent in this oneprettier.requireConfig is true and no config file existsFix

What does the error look like?

  • Format Document does nothing on a .tsx file

Cause 1 of 4

Prettier is not the default formatter for TypeScript React files

1Check

VS Code does not accept combined language keys for editor.defaultFormatter, so a block such as [typescript][typescriptreact] is ignored.

grep -n -B1 -A3 "defaultFormatter" .vscode/settings.json

2Fix

Give each language its own block.

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

3Verify

Run Format Document on the .tsx file; no formatter picker appears.

// Command Palette
Format Document

Confirmed by github.com

Did this fix it?

Cause 2 of 4

Format on save is not on for that language

1Check

The extension follows editor.formatOnSave, which can be set per language.

grep -n "formatOnSave" .vscode/settings.json

2Fix

Turn it on for TypeScript React.

{
  "[typescriptreact]": { "editor.formatOnSave": true }
}

3Verify

Add a badly spaced line to a .tsx file and save.

// the line is reformatted on save

Confirmed by github.com

Did this fix it?

Cause 3 of 4

The file is ignored by .prettierignore

1Check

The extension reads .prettierignore by default, and files in node_modules are ignored.

cat .prettierignore

2Fix

Remove the pattern that matches your file. To format an ignored file once, use the Format Document (Forced) command.

// Command Palette
Format Document (Forced)

3Verify

Format the file normally after editing the ignore file.

npx prettier --check src/App.tsx

Confirmed by github.com

Did this fix it?

Cause 4 of 4

prettier.requireConfig is true and no config file exists

1Check

With requireConfig set to true, Prettier formats a file only if a config file exists.

grep -rn "requireConfig" .vscode/settings.json; ls .prettierrc*

2Fix

Create a config file, or set the option to false.

{
  "prettier.requireConfig": false
}

3Verify

Run Format Document on the .tsx file.

// Command Palette
Format Document

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Commit .vscode/settings.json with one defaultFormatter block per language the project uses.
  • Run prettier --check in CI, so formatting does not depend on every editor being set up.
  • Add a .prettierrc to the repo so requireConfig never silently disables the extension.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Web apps

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.