prettier
Format code consistently using Prettier
TLDR
Format a file and print the result to stdout
Check if a specific file has been formatted
Run with a specific configuration file
Format a file or directory, replacing the original
Format files or directories recursively using single quotes and no trailing commas
Format JavaScript and TypeScript files recursively, replacing the original
SYNOPSIS
prettier [options] [file/directory...]
PARAMETERS
--write, -w
Formats specified files in place, overwriting them. This is one of the most common options.
--check
Checks if files are formatted according to Prettier's rules without writing to disk. Exits with a non-zero code if unformatted files are found.
--list-different, -l
Lists files that are not formatted according to Prettier's rules, without writing to disk.
--stdin
Reads code from standard input (stdin) and prints the formatted output to stdout. Useful for piping content.
--config
Specifies the path to a Prettier configuration file (e.g., .prettierrc) to use. By default, Prettier searches for one.
--ignore-path
Specifies a path to a file containing patterns of files/directories to ignore, similar to .gitignore (default is .prettierignore).
--print-width
Sets the line length at which Prettier will wrap code (default: 80 characters).
--tab-width
Specifies the number of spaces per indentation level (default: 2 spaces).
--use-tabs
Indents with tabs instead of spaces.
--single-quote
Uses single quotes instead of double quotes for strings.
--trailing-comma
Controls the printing of trailing commas (e.g., 'es5' for objects, arrays, etc., where valid in ES5).
--semi, --no-semi
Controls whether to print semicolons at the end of statements (default: --semi).
--parser
Specifies the parser to use. Prettier typically infers this from the file extension, but it can be overridden (e.g., 'typescript', 'json').
--version, -v
Prints the installed Prettier version.
--help, -h
Displays help information for the command.
DESCRIPTION
Prettier is an opinionated code formatter that supports a wide range of programming languages including JavaScript, TypeScript, JSX, HTML, CSS, JSON, Markdown, and more.
Its primary goal is to ensure consistent code style across an entire codebase by parsing your code and reprinting it with its own rules, rather than applying a diff. This eliminates debates over style, as Prettier takes care of all formatting, making code reviews focused on logic rather than aesthetics.
It's commonly used in modern web development workflows, often integrated into Git pre-commit hooks, IDEs, and build processes to automatically format code upon saving or committing.
CAVEATS
Prettier is not a native Linux command; it requires Node.js and npm (or Yarn) to be installed on the system. It is an opinionated formatter, meaning it enforces a specific style which might not always align with every developer's personal preferences without specific configuration. Using --write can overwrite files, so it's advisable to use it with version control. Initial adoption in large, unformatted codebases might lead to significant diffs upon first application.
CONFIGURATION FILES
Prettier automatically looks for configuration files like .prettierrc (in JSON, YAML, or JS format) or a prettier key in package.json in the project root. These files allow users to define specific formatting rules that override Prettier's defaults, ensuring project-specific style consistency.
EDITOR AND IDE INTEGRATION
Prettier is widely integrated into popular text editors and IDEs (e.g., VS Code, WebStorm, Sublime Text, Atom) via plugins. These integrations allow for automatic formatting on save, or on command, making it a seamless part of the development workflow and instantly applying formatting rules.
GIT HOOKS INTEGRATION
It's common practice to integrate Prettier into Git pre-commit hooks using tools like lint-staged and husky. This ensures that all code committed to a repository is automatically formatted before it's pushed, maintaining a consistent style across all contributions and preventing unformatted code from entering the codebase.
HISTORY
Prettier was created by James Kyle and first released in 2017. It quickly gained popularity within the JavaScript ecosystem due to its 'opinionated' nature, which removed the need for endless debates over code style by automating formatting entirely. Its declarative approach to code formatting, where it parses code into an Abstract Syntax Tree (AST) and then re-prints it, set it apart from traditional linters that often just checked for style violations. Over time, it expanded its support to many other languages, becoming a ubiquitous tool in modern development.
SEE ALSO
eslint(1), black(1), clang-format(1), gofmt(1), npm(1)