liveBook · Manning

Prettier

As explained in Secrets of the JavaScript Ninja, Third Edition

Prettier is an opinionated source-code formatter for JavaScript and related languages.

Prettier is an opinionated code formatter that automatically makes code more consistent and readable. It breaks up long lines and applies formatting rules, replacing manual cleanup and team debates with a shared default. section 1.3.2 section 2.3

Add Prettier to a project and run it from the command line:

  • Install it as a development dependency, then create an empty .prettierrc file to keep configuration project-local and signal that Prettier is enabled. section 2.3.1
  • Run npx prettier --write . to format project files. Use .prettierignore to exclude files such as package-lock.json. section 2.3.1
Listing 2.13 A Prettier-formatted JavaScript module
const generateNinjaTurtleAction = () => { 
  const randomNumber = Math.random();
  if (randomNumber < 0.1) {
    return "Fight Shredder";
  } else if (randomNumber < 0.5) {
    return "Rescue April";
  } else {
    return "Eat pizza";
  }
};

A messy JavaScript module becomes consistently indented, spaced, and punctuated after formatting. section 2.3.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains installation, command-line formatting, ignore files, and common options, but not Prettier’s internal formatting algorithm.
Examples
Formats a messy JavaScript module and demonstrates project setup and ignored files.
What it assumes
Assumes Node.js and the node, npm, and npx commands are already available.
What you can do after
Apply Prettier to format project files from the command line and exclude selected files.
How it connects
Contrasts Prettier’s focused formatting role with ESLint’s customizable code rules.

Also explained in

Compare how 2 books explain Prettier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.