PhpStorm 2019.3 Help

Prettier

Prettier is a tool to format .js, .ts, .css, .less, .scss, .vue, and .json code. 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.

Before you start

  1. Install and enable the Prettier plugin on the Plugins page as described in Installing plugins from repository.

  2. Download and install Node.js.

To install Prettier

  • In the embedded Terminal (Alt+F12), 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.

To configure Prettier in PhpStorm

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Languages and Frameworks | JavaScript | Prettier.

  2. On the Prettier page, that opens, specify the path to the prettier package and choose the Node.js interpreter to use. This can be a local Node.js interpreter or a Node.js on Windows Subsystem for Linux.

To reformat code with Prettier

  • Select the code fragment to reformat in the editor or select a file or a folder in the Project tool window and press N/A or select Reformat with Prettier from the context menu.

  • Alternatively, press Ctrl+Shift+A and select Reformat with Prettier from the Find Action list.

    ws_reformat_with_prettier_js.png

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.

To apply Prettier code style rules

  • 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.

Last modified: 9 December 2019