JetBrains Rider 2020.1 Help

Prettier

Prettier is a tool to format .js, .ts, .css, .less, .scss, .vue, and .json code. With JetBrains Rider, you can format selected code fragments as well as entire files or directories using the Reformat with Prettier action. JetBrains Rider adds this action as soon as you install Prettier as a dependency in your project or globally on your computer.

You can also configure Prettier to reformat specific files every time such file is changed and the changes are saved automatically or manually. See Reformat code with Prettier for details.

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.

Install and configure Prettier in JetBrains Rider

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

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

  3. Choose the Node.js interpreter to use. This can be a local Node.js interpreter or a Node.js on Windows Subsystem for Linux.

  4. From the Prettier package list, select the prettier installation to use.

  5. To run Prettier automatically against specific files every time such file is changed and saved, select the Run on save for files checkbox, and specify the pattern that defines the set of files to be reformatted every time such file is saved. See Run Prettier automatically on save.

Reformat code with Prettier

  • In the editor, select the code fragment to reformat and select Reformat with Prettier from the context menu. To reformat a file or a folder, select it in the Project tool window and select Reformat with Prettier from the context menu.

You can also configure Prettier to reformat specific files every time such file is changed and the changes are saved automatically or manually. 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/Preferences dialog Ctrl+Alt+S, go to Languages and Frameworks | JavaScript | Prettier, select the Run on save for files checkbox, and specify the pattern that defines the set of files to be reformatted every time such file is saved.

  2. You can accept the default pattern or type a custom one.

    With the default pattern, {**/*,*}.{js,ts,jsx,tsx}, 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,css,scss,sass}

    To reformat files from a specific folder, 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}

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

Configure Prettier to run automatically on save in new projects

  • Go to File | New Projects Settings | Settings for New Projects. In the dialog that opens, go to Languages and Frameworks | JavaScript | Prettier, and select the Run on save for files checkbox. If necessary, update the default pattern as described above.

Apply Prettier code style rules

JetBrains Rider can apply the key code style rules from the Prettier's configuration to the JetBrains Rider 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: 29 May 2020