Logo

dev-resources.site

for different kinds of informations.

Format Your Code Using Prettier Like a Pro

Published at
1/8/2025
Categories
webdev
javascript
tutorial
tooling
Author
efrenmarin
Author
10 person written this
efrenmarin
open
Format Your Code Using Prettier Like a Pro

Formatting Cartoon

Don't be like this guy—don’t be a schmuck.

Why Formatted Code Is Needed

Formatting helps structure the lines of code you write making it easier to read and understand. This is crucial when working on a codebase with multiple developers, all with their own style and preference on how their code is structured. Having a uniformed formatted codebase helps prevent headaches when merging and creates a standard that you and your team can build on.

There are several ways to set up a formatting template for yourself and your team. In this article, we’ll explore one of the more popular options: Prettier.

According to the State of JS 2021 survey, 83% of respondents regularly use Prettier as their formatter of choice, a 13% increase from the previous year's survey. Many prominent teams—such as those at Facebook, Webflow, Jest, Dropbox, Spotify, and PayPal—use Prettier to ensure consistent formatting in their codebases.

Prettier can be configured and run in multiple ways. In this example, I’ll demonstrate how to set up Prettier with a Git hook for automation in VS Code. For more examples and configurations, visit Prettier’s documentation.


Before You Start

While it’s not required, it’s helpful to understand the options you’ll be configuring and what they do. You’ll need to create two files and place them at the root level of your project. Keep in mind that these formatting options are project-specific, so you’ll need to repeat this process for each new project. These files will contain the options you can choose, and you can modify or remove options that don’t fit your project’s needs.


.prettierrc

This file, located at the root level of your project, defines the base formatting rules for Prettier. It uses a JSON structure and can be tailored to your team’s standards. Here’s an example:

.prettierrc


.editorconfig

This file ensures consistency in your editor settings even before Prettier runs. It also covers options that .prettierrc does not. Here’s an example:

.editorconfig


Setting Up the Workflow

For ease of use, both files can be copied at the end of the article. Once you’ve created and configured the .prettierrc and .editorconfig files, you can proceed. Install these three npm packages to streamline the formatting process:

npm install --save-dev prettier lint-staged husky

Then initialize Husky

npx husky init

These steps accomplish the following:

  • Install Prettier for formatting.
  • Install lint-staged to format only staged files before committing.
  • Install Husky to set up Git hooks for automation.
  • Initialize Husky, which creates the necessary dependencies and a pre-commit file.

Configuring New Files

Two additional files need configuration: pre-commit and .lintstagedrc

pre-commit

This file, automatically created by Husky, guides the automation process. It is located inside the Husky folder created during initialization. Configure it as shown below:

pre-commit

.lintstagedrc

Create this file (with no extension similar to .editorconfig and .prettierrc) in the root project folder. It narrows the scope of files Prettier formats through the Git hook. Below is an example, but you can adjust it based on your project’s file types:

.lintstagedrc


Testing the Workflow

Once all four files are in place—.prettierrc, .editorconfig, .lintstagedrc, and pre-commit—you can test the workflow.

  1. Make a simple formatting change to a .js file (add unnecessary spaces or indents, for example).
  2. Stage your changes: git add -A
  3. Commit with a test message git commit -m "Testing formatting workflow"

If everything was set up correctly, your terminal should display a success message, and the formatting changes will be automatically applied.

Console example


That's it!

You now have a simple yet effective way to harness Prettier and git hooks to automate the formatting of your code.

I’d love to hear your thoughts! Let me know in the comments if this was helpful or if you encounter any issues—I’m here to help!


Copy Paste File Examples

.prettierrc

{
  "arrowParens": "always",
  "bracketSameLine": false,
  "bracketSpacing": true,
  "embeddedLanguageFormatting": "auto",
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "preserve",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false
}
Enter fullscreen mode Exit fullscreen mode

.editorconfig

# Top-most EditorConfig file
root = true

# Global settings
[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# Overrides
[*.md]
trim_trailing_whitespace = false
max_line_length = off

[*.yml]
indent_style = space
indent_size = 2

[*.ts]
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab

[*.html]
indent_size = 2

[*.json]
indent_size = 2

Enter fullscreen mode Exit fullscreen mode
tooling Article's
30 articles in total
Favicon
ruby -run
Favicon
Top 10 Online Postman-Like Tools for API Testing and Development
Favicon
ruby -run, again
Favicon
Make Better Decisions as a Software Engineer Using the Pugh Matrix
Favicon
💡 How Do You Generate Your Cover Images for Blog Posts?
Favicon
Here are 7 Regex tools that can save your life from hell 🔥
Favicon
OnyX: The Best Free Mac Cleanup Tool You’ll Ever Need
Favicon
Like IDE for SparkSQL: Support Pycharm! SparkSQLHelper v2025.1.1 released
Favicon
TikTok Banned? The REDnote App Helps You Keep Creating and Connecting!
Favicon
Remove plugins affecting Intellij idea
Favicon
SEO AI: Your Secret Weapon for Boosting SEO Performance!
Favicon
AI translator
Favicon
saas opesource tools
Favicon
ArtenoAPI: Translation, Geolocation, QR Codes, and More in One API
Favicon
Top DevSecOps Tools for 2025
Favicon
Innovative Strategies for Community Collaboration Platforms
Favicon
Navigating the Content Maze: A Startup's Guide to Effective Multi-Channel Distribution
Favicon
The Power of Storytelling in Nonprofit Campaigns
Favicon
How to Use Powerdrill AI to Make a Literary Analysis
Favicon
The Hidden Challenges of Cross-Border E-Commerce and How to Overcome Them
Favicon
Mastering Developer Documentation: A Journey Beyond the Basics
Favicon
10 Figma Shortcuts to Design Faster
Favicon
Navigating the Complexities of Open-Source Tool Architecture: Advanced Strategies for Success
Favicon
Unlocking the Secrets of Content Growth Optimization: Strategies for Success
Favicon
Crafting a Cohesive Brand Voice Across Multiple Channels: Strategies for Success
Favicon
10 Must-Have Free Tools for Creative Designers
Favicon
Making Video Creation Easy? InVideoAI.video Has the Answer
Favicon
A Developer's Guide to Intelligent Workflow Automation
Favicon
Format Your Code Using Prettier Like a Pro
Favicon
Designing Technical Training that Drives Real Results

Featured ones: