PhpStorm 2023.3 Help

Prettier

Prettier is a tool to format files in various languages, like TypeScript, JavaScript, CSS, HTML, JSON, and others. With PhpStorm, you can format selected code fragments as well as entire files or directories using the Reformat with Prettier action. PhpStorm adds this action as soon as you install Prettier as a dependency in your project or globally on your computer.

You can configure Prettier to reformat specific files every time such file is changed and the changes are saved automatically or manually, refer to Run Prettier automatically on save.

Also, Prettier can be set as default formatter for specific files. It will run against such files every time you reformat your code with Ctrl+Alt+L.

For more information, refer to Reformat code with Prettier.

Before you start

  1. Download and install Node.js.

  2. Make sure a local Node.js interpreter is configured in your project: open the Settings dialog (Ctrl+Alt+S) and go to Languages & Frameworks | Node.js. The Node interpreter field shows the default project Node.js interpreter.

    Learn more from Configuring a local Node.js interpreter.

  3. Make sure the JavaScript and TypeScript and Prettier required plugins are enabled on the Settings | Plugins page, tab Installed. For more information, refer to Managing plugins.

Install Prettier

  1. Open the embedded Terminal (Alt+F12) .

  2. Type one of the following commands:

    • npm install --save-dev --save-exact prettier

    • npm install --global prettier

Learn more about installation modes from the Prettier official website.

Configure Prettier in PhpStorm

  1. In the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | JavaScript | Prettier.

  2. PhpStorm enables Prettier and adds the Reformat with Prettier action as soon as you install it as described above. To turn Prettier off, select Disable Prettier.

  3. Choose how to detect the Prettier package and configuration file:

    • Automatic Prettier configuration: In this mode, PhpStorm uses the Prettier package from the closest node_modules (located in the same folder as a file to reformat or on its parent folders) and looks for the closest .prettierrc.* configuration file.

      As a result, if your project consists of multiple modules with different Prettier configurations, each module will use the rules from its own Prettier configuration file (if such config is found).

    • Manual Prettier configuration: In this mode, the Prettier package and the rules from the specified configuration file will be applied to all modules in your project.

      From the Prettier package list, select the prettier installation to use. If you followed the standard installation procedure, PhpStorm locates the prettier package itself and the field is filled in automatically.

      Configure Prettier: manual mode

      To set Prettier as the default formatting tool, select the Run on 'Reformat Code' action checkbox.

  4. To apply Prettier automatically for saved files, select the On save checkbox.

  5. In the Run for files field, specify the file patterns to which Prettier will be applied automatically when such files are saved or when the Reformat Code action is invoked.

    For more information, refer to Run Prettier automatically on save and Set Prettier as default formatter.

Reformat code with Prettier

  1. In the editor, select the code fragment to reformat. To reformat a file or a folder, select it in the Project tool window.

  2. Then select Reformat with Prettier from the context menu.

You can configure Prettier to reformat specific files every time such file is changed and the changes are saved automatically or manually, refer to Run Prettier automatically on save.

Also, Prettier can be set as default formatter for specific files. It will run against such files every time you reformat your code with Ctrl+Alt+L.

This behaviour can be enabled in the current project as well as for all new projects.

Run Prettier automatically on save

  1. Open the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | JavaScript | Prettier, and select the Run on save checkbox.

  2. In the Run for files field, specify the pattern that defines the set of files to be reformatted every time such file is saved. You can accept the default pattern or type a custom one.

    With the default pattern, **/*.{js,ts,jsx,tsx,vue,astro}, Prettier will wake up and process any updated and saved JavaScript, TypeScript, JSX, or TSX file. To reformat files of other types or files stored in specific folders, use glob patterns to update the default pattern.

    • For example, to automatically reformat Style Sheet files as well, add css,sass,scss to the default pattern as follows:

      **/*.{js,ts,jsx,tsx,vue,astro,css,scss,sass}
    • To reformat files from a specific folder, including subfolders, replace **/* with <path to the folder>/**/*.

      Suppose, you have a project with the following structure:

      Prettier: custom patterns. Example project structure

      To apply Prettier automatically only to the files in the src folder, update the pattern as follows:

      src/*.{js,ts,jsx,tsx,vue,astro}

      As a result, the file dog.ts will be reformatted on save while animal.ts will remain unchanged.

Set Prettier as default formatter

  1. Open the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | JavaScript | Prettier.

  2. Select Manual Prettier configuration and then select the Run on 'Reformat Code' action checkbox. Learn more about Prettier configuration from Choose how to detect the Prettier package and configuration file.

  3. In the Run for files field, specify the pattern that defines the set of files to be always reformatted with Prettier. Accept the default pattern or customize it as described in Run Prettier automatically on save.

Configure Prettier to run on save or on reformat in new projects

  1. Go to File | New Projects Setup | Settings for New Projects. In the dialog that opens, go to Languages & Frameworks | JavaScript | Prettier.

  2. Use the Run on 'Reformat Code' action and On save checkboxes to specify the actions that will trigger Prettier.

  3. If necessary, update the default pattern in the Run for files field as described in Run Prettier automatically on save.

Apply Prettier code style rules

PhpStorm can apply the key code style rules from the Prettier's configuration to the PhpStorm Code Style settings so that generated code (for example, after refactoring or quick-fix) and the code that is already processed with Prettier are formatted consistently.

  • In the project where Prettier is enabled, open package.json and click Yes in the pane at the top of the tab.

    Pane above package.json: apply Prettier code style
  • To re-apply the Prettier code style (after you've clicked No in the pane or modified the code style), press Ctrl+Shift+A and select Apply Prettier Code Style Rules from the Find Action list.

Learn more about configuring Prettier code style rules from the Prettier official website.

Last modified: 25 March 2024