Loading component...

Published on March 21, 2023Reading time: 4 min read

Running Prettier from the Command Line

Loading component...

Related Articles

SitecoreAI

Sitecore Workflow Automation with Management API

A practical guide to managing Sitecore workflows programmatically using the Management GraphQL API with production-tested patterns.

November 20, 20251 min read
Blog Card Placeholder
Dev

How to Create a New Sitecore SXA Headless Site

SXA Headless is easy, awesome and powerful! Learn how to create your first Sitecore Headless Tenant & Site with SXA.

November 19, 20251 min read

Loading component...

Digital solutions

Learn how to maximize your investment in Sitecore by working with us.

AI solutions

Dominate your industry. Work with us to digitally transform your business.

Fishtank's proprietary project accelerator. Save time and money. Build what matters.

Utilities

Energy

Education

Healthcare

Manufacturing

Professional services

Financial services

Non-profits and associations

About FishtankThe PlaybookSitecore SnackContact us

Solutions

  • Digital solutions
  • AI solutions
  • TIDAL for XM Cloud

About

  • About Fishtank
  • The Playbook
  • Sitecore Snack
  • Contact us

Insights

Work

Industries

  • Utilities
  • Energy
  • Education
  • Healthcare
  • Manufacturing
  • Professional services
  • Financial services
  • Non-profits and associations

Start typing to search...

Running Prettier from the Command Line

Prettier has become the household name for code formatting and it's gaining much publicity over the years and millions of developers around the world utilize this powerful tool to format code base for production ready. Currently, based on Visual Studio code IDE, there are over 30 million users around the globe and over 420 companies utilizing this tool according to stackshare and the increase in utility is still counting.

Prettier is an opinionated code formatter with support for some programming languages, frontend frameworks and CSS frameworks. It is Eslint on steroids, fully automatic, it enforces code styling consistency across the code base. This is made possible by its ability to parse away original styling and re-print the parsed AST (Abstract Syntax Tree) with its own rules that take the maximum line length into account and wrap codes when it is necessary.

Benefits to a Developer?

Prettier have helped to speed up code writing where an individual does not have to worry about code formatting which used to be a developer’s nightmare, it is customizable, runs offline and is completely free. It boosts up developer’s accuracy level, knowing that Prettier got you! No matter how messy the code is, prettier properly configured in your IDE will format all messy code in a blink.

Prettier Installation

Prettier can be installed through VS Code IDE extensions by searching for “Prettier - code formatter” in visual studio code marketplace and clicking on it. When it is opened, you can review it and click the install button. This is available in VSCode IDE. You can also download it via visual studio code marketplace.

Prettier Code Formatter

Prettier Code Formatter download

Prettier can also be installed in VS Code by launching VS Code Quick Open (Ctrl + P), and paste the following command and hit enter.

ext install esbenp.prettier-vscode

Prettier Code Formatter Step

If you have a pre-installed formatter in your IDE, to ensure this extension is used over other extensions, set Prettier as the default formatter in your IDE settings. You can either set it to all languages or by a specific language of your choosing.

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

If you want to exempt a particular language to not use Prettier as a default formatter, you can have your settings this way.

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[javascript]": {
    "editor.defaultFormatter": "<another formatter>"
  }
}

Additionally, you have the option of disabling format on save for specific languages if you do not want autosave on them. Simply set the editor.formatOnSave to false

{
  "[javascript]": {
    "editor.formatOnSave": false
  }
}

For Prettier to run in your terminal, you must install Prettier in your project’s local dependencies.

  • npm / yarn

      npm install --save-dev --save-exact prettier
    
      yarn add --save-dev --save-exact prettier
    

After the installation is complete, create an empty config file.

echo {}> .prettierrc.json

You can create this file manually in your project root directory and populate the file based on preference.

Prettier Code Formatter in Visual Studio Code

Next is to create a .prettierignore file in your project root directory and specify which file to not format.

# Ignore artifacts:
build 
coverage

# Ignore all HTML files:
*.html

Tip: Add your .prettierignore file inside your .gitignore file and .eslintignore if they exist.

For ease of command run, add the following command script to your script object in your package.json file specifying the files to target by providing the path to the file inside of the curly bracket. The example shown below:

"format": "prettier --loglevel warn --write \"{<file-path>}/**/*.{jsx,js}\"",

Prettier Code Formatter example in Visual Studio Code

The example above is from a project on a mac os.

Now format all files with Prettier by running - npm run format in your terminal. This command will pick up “format” inside “script” in your project’s package. JSON file.

npm run format

Alternatively;

NPM

npx prettier --write .

YARN

yarn prettier --write .

This will apply prettier to all files in your project. If you only want to perform this command on a particular folder, you can “cd” into the folder and run the command.

Prettier Resources:

Prettier Documentation

Why Prettier?

Solutions

  • Digital solutions
  • AI solutions

Brands

  • TIDAL for SitecoreAI
  • Sitecore Snack
  • The Playbook

Fishtank lore

  • Work
  • Insights
  • About us
  • Contact us

Industries

Utilities
Professional services
Energy
Financial services
Healthcare
Education
Non-profits and associations
Manufacturing
Utilities
Professional services
Energy
Financial services
Healthcare
Education
Non-profits and associations
Manufacturing

2025 @All Rights Reserved. Privacy Policy.

FISHTANK