How to Use Prettier to Format Your Code: A Comprehensive Guide

Thiraphat Phutson
3 min readJun 5, 2024

--

Writing clean, consistent, and readable code is crucial for maintaining a high-quality codebase. Prettier is a powerful code formatter that helps developers achieve this by automatically formatting their code according to a set of predefined rules. In this guide, we will explore how to set up and use Prettier to format your code effortlessly.

What is Prettier?

Prettier is an opinionated code formatter that supports multiple programming languages and integrates with various code editors and build systems. It ensures that your code follows consistent style guidelines, making it easier to read and maintain.

Why Use Prettier?

  • Consistency: Ensures that all code follows the same formatting rules.
  • Time-Saving: Automatically formats code, reducing the need for manual formatting.
  • Collaboration: Makes it easier for teams to work together by adhering to a common style.
  • Error Reduction: Helps catch formatting errors early in the development process.

Setting Up Prettier

To get started with Prettier, follow these steps:

Install Prettier You can install Prettier globally or as a dev dependency in your project. To install it globally, run:

npm install -g prettier

To install it as a dev dependency, run:

npm install --save-dev prettier

Create a Configuration File Create a .prettierrc file in the root of your project to define your formatting rules. Here’s an example configuration:

{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}

Add a Format Script Add a script to your package.json to format your codebase:

"scripts": {
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}\""
}

Run Prettier To format your code, run the following command:

npm run format

Integrating Prettier with Your Editor

Prettier can be integrated with various code editors to format your code on save. Here are instructions for popular editors:

Visual Studio Code (VS Code)

  • Install the Prettier extension from the VS Code marketplace.
  • Open your settings and enable format on save:
"editor.formatOnSave": true,
"prettier.requireConfig": true

WebStorm

  • Open Preferences and navigate to Languages & Frameworks > JavaScript > Prettier.
  • Enable On code reformat and On save.

Atom

  • Install the prettier-atom package.
  • Open package settings and enable format on save.

Customizing Prettier

Prettier offers various options to customize the formatting rules according to your preferences. Some common options include:

  • printWidth: Specifies the line length that the formatter will wrap on. Default is 80 characters.
  • tabWidth: Specifies the number of spaces per indentation level. Default is 2.
  • useTabs: If true, Prettier will use tabs for indentation instead of spaces.
  • semi: If true, Prettier will add a semicolon at the end of every statement. Default is true.
  • singleQuote: If true, Prettier will use single quotes instead of double quotes. Default is false.

Refer to the official Prettier documentation for a full list of configuration options.

Prettier in Continuous Integration (CI)

You can integrate Prettier into your CI pipeline to ensure that code is consistently formatted before it is merged. Here’s an example using GitHub Actions:

name: Prettier Check

on: [push, pull_request]

jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run Prettier
run: npm run format:check

Conclusion

Prettier is an essential tool for any developer looking to maintain a clean and consistent codebase. By automating the formatting process, Prettier saves time, reduces errors, and makes it easier to collaborate with others. Set up Prettier in your project today and experience the benefits of automated code formatting.

--

--

Thiraphat Phutson
Thiraphat Phutson

Written by Thiraphat Phutson

I'm a software developer committed to improving the world with technology. I craft web apps, explore AI, and share tech insights.