Course contentsShow
← Web Development
Lesson 1455 of 2,98226. Code Quality ToolsFree lesson

Installing and Running Prettier

Setting up Prettier in a project, running it via CLI, and understanding the basic formatting workflow.

Installing and Running Prettier

You'll learn: How to add Prettier to your project and use it to automatically format your code.

Adding Prettier to Your Project

Now that you understand what Prettier does, it's time to get it running. Installing Prettier is straightforward:

npm install --save-dev prettier

This adds Prettier as a development dependency—you don't need it in production since it only formats source code during development.

Running Prettier via CLI

Once installed, you can format files directly from the command line:

# Check if files are formatted (doesn't change anything)
npx prettier --check .

# Actually format files
npx prettier --write .

# Format specific files or patterns
npx prettier --write "src/**/*.{js,css,html}"

Think of it like this: --check is like a spelling checker that highlights errors, while --write is like autocorrect that fixes them automatically.

Basic Formatting Workflow

Here's a typical workflow:

  1. Write your code however you naturally type it
  2. Run Prettier manually or on save (via editor integration)
  3. Prettier reformats your code consistently
  4. Commit the formatted code to version control

You can add convenience scripts to your package.json:

{
  "scripts": {
    "format": "prettier --write .",
    "format:check": "prettier --check ."
  }
}

Now you can run npm run format anytime you want to format your entire codebase.

Key Takeaway: Installing Prettier requires just one npm command, and running it is as simple as npx prettier --write . to format your entire project—setting up scripts makes this even more convenient for daily use.